Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..
C#
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
Comments
[no name] 21-Dec-12 8:49am    
Where's your dropdown ???

1 solution

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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900