Ok are you talking about storing multiple selected values in dropdownlist in DB?
For this it's better to have another table in DB, which would store the foreign key and multiple values.
As this is one to many relationship you'd need another table to store all rows or else if you want to store all rows in the same table it would lead to data redundency.
apurva16
Member
21 Points
87 Posts
how to save checked item into database.
Apr 06, 2012 05:42 AM|LINK
hello,
i want to insert a checked item into a database.how can i do this?
thanx.
nijhawan.sau...
All-Star
16398 Points
3172 Posts
Re: how to save checked item into database.
Apr 06, 2012 05:49 AM|LINK
Hi,
You mean value of a checkbox field?
In code just give a condition saying if checkbox1.isChecked==true then write your DB query to do a insert or what ever you want to do.
nijhawan.sau...
All-Star
16398 Points
3172 Posts
Re: how to save checked item into database.
Apr 06, 2012 05:49 AM|LINK
Hi,
You mean value of a checkbox field?
In code just give a condition saying if checkbox1.isChecked==true then write your DB query to do a insert or what ever you want to do.
apurva16
Member
21 Points
87 Posts
Re: how to save checked item into database.
Apr 06, 2012 05:53 AM|LINK
check the following code.i want to insert checked value in db.so how i can i write the code?
SqlConnection con = new SqlConnection(strConnection); con.Open(); cmd = new SqlCommand("Insert Into UserCreation(FullName,UserName,Email,PhoneNo,RegionID,StateId,CityID,Password,SecurityQue,SecurityAns) VALUES(@FullName,@UserName,@Email,@PhoneNo,@RegionID,@StateId,@CityID,@Password,@SecurityQue,@SecurityAns)", con); cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@FullName", txtFullname.Text); cmd.Parameters.AddWithValue("@UserName", txtUserName.Text); cmd.Parameters.AddWithValue("@Email", txtEmail.Text); cmd.Parameters.AddWithValue("@PhoneNo", txtPhoneno.Text); cmd.Parameters.AddWithValue("@RegionID", DdlRegion.SelectedValue); cmd.Parameters.AddWithValue("@StateId", DdlState.SelectedValue); cmd.Parameters.AddWithValue("@CityID", DdlCity.SelectedValue); cmd.Parameters.AddWithValue("@Password", txtPassword.Text); cmd.Parameters.AddWithValue("@SecurityQue", txtSecurityquestion.Text); cmd.Parameters.AddWithValue("@SecurityAns", txtSecurityanswer.Text); //cmd.Parameters.AddWithValue("MenuMasterID",chkmenuRights.SelectedItem); cmd.ExecuteNonQuery(); con.Close(); ClientScript.RegisterStartupScript(this.GetType(), "Hello", "alert('Successfully created a user');", true); txtFullname.Text = ""; txtUserName.Text = ""; txtEmail.Text = ""; txtPhoneno.Text = ""; txtPassword.Text = ""; txtConfirmpassword.Text = ""; txtSecurityquestion.Text = ""; txtSecurityanswer.Text = ""; DdlRegion.SelectedIndex = 0; DdlState.SelectedIndex = 0; DdlCity.SelectedIndex = 0; }nijhawan.sau...
All-Star
16398 Points
3172 Posts
Re: how to save checked item into database.
Apr 06, 2012 06:22 AM|LINK
CAn you tell which particular control you are talking about in your code?
apurva16
Member
21 Points
87 Posts
Re: how to save checked item into database.
Apr 06, 2012 06:28 AM|LINK
not getting what you want?i want to insert selected item into db.means suppose i cliked 2 item then these item inserted into db.see the code.
nijhawan.sau...
All-Star
16398 Points
3172 Posts
Re: how to save checked item into database.
Apr 06, 2012 06:33 AM|LINK
Ok are you talking about storing multiple selected values in dropdownlist in DB?
For this it's better to have another table in DB, which would store the foreign key and multiple values.
As this is one to many relationship you'd need another table to store all rows or else if you want to store all rows in the same table it would lead to data redundency.
apurva16
Member
21 Points
87 Posts
Re: how to save checked item into database.
Apr 06, 2012 06:37 AM|LINK
exactly.i alreday made a 2 table
1)menumaster in this menumasterid,menu caption.in menu caption i stored the all pages forex.login,ticket status,new ticket etc.
2)menu rights:-in this 3 column made rightsid,userid(foreign),menumasterid(foreign).
and in usercraetion i taked checkbox list and bounded a menucaption.now when i select menurights then it inserted to a table.
hope u get it.
thanx.
nijhawan.sau...
All-Star
16398 Points
3172 Posts
Re: how to save checked item into database.
Apr 06, 2012 10:23 AM|LINK
Ok so you need to loop through all selelcted values inside the list and fire a Insert query in the DB.
foreach(ListItem listItem in listBox1.Items)
{
if (listItem.Selected == True)
{
// Insert statemenet goes here.
}
}
apurva16
Member
21 Points
87 Posts
Re: how to save checked item into database.
Apr 07, 2012 09:23 AM|LINK
hi,
pls tell me how can i write this code to my code?i posted above my code pls help.
Thanx,
apurva.