Click here to Skip to main content
11,645,087 members (63,184 online)
Rate this: bad
good
Please Sign up or sign in to vote.
I have A drop Down list and i want that if selected nothing the id should be stored in database 0 other wise if selected something then the id of that item .??
code..
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
 
           Query = @"SELECT [category_id] FROM [Category_Master]";
           ds1 = dl.fetchrecord(Query);
           ddl_parent.Items.Insert(0, new ListItem("---none---"));
           int i = 0;
           while (i < ds1.Tables[0].Rows.Count)
           {
               flag = 1;
               catname = "";
               index = 1;
               ListItem li = new ListItem();
               li.Text = catmapping(ds1.Tables[0].Rows[i]["category_id"].ToString());
               li.Value = ds1.Tables[0].Rows[i]["category_id"].ToString();
               ddl_parent.Items.Add(li);
               i = i + 1;
        }
       }
       lbl_mess.Text = "";
   }
 

   string catmapping(string cat_id)
   {
       while (flag == 1)
       {
           Query = "select Category_ParentId,Category_Name from Category_Master where category_id=" + cat_id + "";
           ds = dl.fetchrecord(Query);
           cat_id = ds.Tables[0].Rows[0]["Category_ParentId"].ToString();
           str = ds.Tables[0].Rows[0]["Category_Name"].ToString();
           if (index == 1)
           {
               catname = str;
           }
           else
           {
               catname = str + ">>" + catname;
           }
           if (cat_id == "0")
           {
               flag = 0;
           }
           index++;
       }
 
       return catname;
   }
 
   protected void Button1_Click(object sender, EventArgs e)
   {
 
       if (IsPostBack)
       {
           Query = @"INSERT INTO [RbmDatabase].[dbo].[Category_Master]
          ([Category_ParentId]
          ,[Category_Name]
          ,[Category_MetaTag]
          ,[Category_MetaTagKeywords]
          ,[Category_Description]
          ,[Category_SortOrder]
          ,[Category_Status]
          ,[Category_UpdateDate])
    VALUES ('" + dl.QoutesHandel(ddl_parent.SelectedValue.ToString()) + "','" + dl.QoutesHandel(txt_category.Text.Trim()) + "','" + dl.QoutesHandel(txt_MTdesc.Text.Trim()) + "','" + dl.QoutesHandel(txt_MTkey.Text.Trim()) + "','" + dl.QoutesHandel(txt_Desc.Text.Trim()) + "','" + dl.QoutesHandel(txt_order.Text.Trim()) + "','" + dl.QoutesHandel(ddl_status.SelectedIndex.ToString()) + "','" + String.Format("{0:yyyy-MM-dd}", DateTime.Now) + "')";
           dl.insertrecord(Query, lbl_mess);
           lbl_mess.Text = "Record Inserted  Successfull.....!";
 
       }
 
   }
Posted 21-Dec-12 0:58am
Comments
Krunal Rohit at 21-Dec-12 8:49am
   
Where's your dropdown ???

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here is an example how i have done this recently:

using (SqlConnection connDB = new SqlConnection())            
using(SqlDataAdapter adapter = new SqlDataAdapter())
{
	connDB.ConnectionString = @"Server=SRV-WEB\SQLEXPRESS;Database=????;User Id=?????;Password=?????;";
	adapter.SelectCommand = connDB.CreateCommand();
	adapter.SelectCommand.CommandText = "SELECT [ConsultantID],[name] FROM [dbo].[ConsultantOptions] WHERE Enabled = 1 ORDER BY [name] ASC; ";
 
	using(DataTable data = new DataTable())
	{                    
		adapter.Fill(data);
		data.Rows.Add(new string[] { "0", "All" });
 
		DataView view = data.AsDataView();                    
		view.Sort = "name";
 
		this.cboConsultantName.DataSource = view;
		this.cboConsultantName.DataTextField = "name";
		this.cboConsultantName.DataValueField = "ConsultantID";
		this.cboConsultantName.DataBind();
		this.cboConsultantName.SelectedValue = "All";
 
	}
}

To reference the ID value I would use the following line:

this.cboConsultantName.SelectedItem.Value
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 700
1 jyo.net 494
2 Sergey Alexandrovich Kryukov 449
3 Afzaal Ahmad Zeeshan 444
4 CPallini 386
0 OriginalGriff 1,065
1 Sergey Alexandrovich Kryukov 683
2 DamithSL 681
3 Afzaal Ahmad Zeeshan 662
4 CPallini 555


Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 21 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100