Reputation: 11
I have a drop Down list and I want that if the user selected nothing the id should be stored in database as 0, other wise if the user selected something then the id of that item is stored. Code is here:
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 = "";
}
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.....!";
}
}
Upvotes: 1
Views: 57
Reputation: 66
You should insert the "none" value when creating the list item
new ListItem("---none---", "0"));
Upvotes: 2