i am binding the datalist using this code and store procedure. the code works fine if i pass only 1 id, but i have an array which have multiple id's separated by comma like 1,3,5, and i want to use this in the @Id parameter , but i get below conversion error: An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Conversion failed when converting the varchar value '1, 3, 4, 5, 6, 7' to data type int. Kindly suggest, its urgent. Code behind code is:
private void PageBind(Int32 pno)
{
Int32 nor, repcol;
nor = Convert.ToInt32(DropDownList1.SelectedValue);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Display_Search_Records";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.Add("@pagenumber", SqlDbType.Int).Value = pno;
cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = nor;
// start dummy code.
foreach (DataTable table in ds.Tables)
{
foreach (DataRow theRow in ds.Tables[0].Rows)
{
test += theRow["ID"] + ", ";
}
}
test = test.Substring(0, test.Length - 2).Trim();
cmd.Parameters.Add("@Id", SqlDbType.VarChar, 200).Value = test;
// end.
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
Int32 tot = Convert.ToInt32(dr[0]);
Label1.Text = pno.ToString();
Label2.Text = " of ";
Int32 a = Convert.ToInt32(tot / nor);
if (tot % nor != 0)
{
a += 1;
}
Label3.Text = a.ToString();
if (dr.NextResult())
{
DataList1.DataSource = dr;
DataList1.DataBind();
}
dr.Close();
cmd.Dispose();
btn_first.Enabled = true;
btn_previous.Enabled = true;
btn_next.Enabled = true;
btn_last.Enabled = true;
if (pno == 1)
{
btn_first.Enabled = false;
btn_previous.Enabled = false;
}
if (pno == Convert.ToInt32(Label3.Text))
{
btn_next.Enabled = false;
btn_last.Enabled = false;
}
//For Google like code as 1 2 3 4 5 6
Int32 i;
ArrayList ar = new ArrayList();
for (i = 1; i <= Convert.ToInt32(Label3.Text); i++)
{
ar.Add(i.ToString());
dtlpaging.RepeatDirection = RepeatDirection.Horizontal;
dtlpaging.DataSource = ar;
dtlpaging.DataBind();
}
}
Store Procedure:
USE [L2]
GO
/****** Object: StoredProcedure [dbo].[Display_Search_Records] Script Date: 09/05/2014 14:07:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Display_Search_Records]
(
@pagenumber int,
@pagesize int,
@Id varchar(200)
)
AS
declare @srec int -- Means startingRec
declare @sbid int -- Means startingProductId
declare @erec int -- Means endRec
declare @ebid int -- Means endProductId
declare @rc int -- Means recCount
set @srec = @pagenumber * @pagesize - @pagesize + 1
declare c_cart scroll cursor for select Id from Product where Id IN (@Id) order by Id
open c_cart
fetch absolute @srec from c_cart into @sbid
select @rc = count(*) from Product where Id IN (@Id) and Id > @sbid
if @rc < @pagesize
set @erec = @srec + @rc
else
set @erec = @pagenumber * @pagesize
fetch absolute @erec from c_cart into @ebid
close c_cart
deallocate c_cart
-- select count(*) from Product
select count(*) from Product where Id IN (@Id)
-- select * from Product where Id >= @sbid and Id <= @ebid
select * from Product where Id IN (@Id) and Id >= @sbid and Id <= @ebid