my listbox has severals values. I am trying to pre select my listbox at page load but the problem is it only select the first value.
Suppose i have values in my listbox from 1-10 and in database column i have 1,4,7 then it pre select only 1 in the listbox.
If i have 1,2,7 then it preselect 1 and 2
If i have 1,2,3 then it preselect 1,2 and 3
So it pre select only together values if any gap occur then it select upper value.
<asp:ListBox ID="dd_subcategory" runat="server" CssClass="lbcss" SelectionMode="Multiple">
</asp:ListBox>
private void FillSubCategory(int CategoryID)
{
SqlCommand cmd = new SqlCommand("sps_bindcategory", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@flag", 2);
cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
dd_subcategory.DataSource = objDs.Tables[0];
dd_subcategory.DataTextField = "subcategory_name";
dd_subcategory.DataValueField = "subcategory_id";
dd_subcategory.DataBind();
}
}
if (!Page.IsPostBack)
{
DataTable dt = GetData();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string id = dt.Rows[i]["subcategory_id"].ToString();
if (id == dd_subcategory.Items[i].Value)
{
dd_subcategory.Items[i].Selected = true;
}
}
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
try
{
con.Open();
SqlCommand cmd = new SqlCommand("sps_addetails", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ad_id", ad_id);
cmd.Parameters.AddWithValue("@useremail", ses);
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DA.Fill(dt);
con.Close();
}
catch (SqlException)
{
throw;
}
return dt;
}
ALTER PROCEDURE [dbo].[sps_addetails]
@ad_id int,
@useremail nvarchar(100)
AS
BEGIN
select r.*, sc.subcategory_id from dbo.tbl_adregister r INNER JOIN tbl_adsubcategory sc on r.ad_id=sc.ad_id
where useremail=@useremail and r.ad_id=@ad_id
END