Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir,
I have take two dynamic dropdownlist and one text box.now i want to store this data into sqlserver database.
I have written code but it is not work properly.
I will send my code please help me for this.

this is Add button(to add new row(dropdownlist and textbox)
static int i = 0;
protected void Button1_Click(object sender, EventArgs e)
{

for (int j = 0; j <= i; j++)
{
DropDownList drpstart = new DropDownList();
drpstart.ID = "d1"+j;
drpstart.Items.Add(new ListItem("8AM"));
drpstart.Items.Add(new ListItem("9AM"));
drpstart.Items.Add(new ListItem("10AM"));
drpstart.Items.Add(new ListItem("11AM"));
drpstart.Items.Add(new ListItem("12PM"));
drpstart.Items.Add(new ListItem("1PM"));
drpstart.Items.Add(new ListItem("2PM"));
drpstart.Items.Add(new ListItem("3PM"));
drpstart.Items.Add(new ListItem("4PM"));
drpstart.Items.Add(new ListItem("5PM"));
drpstart.Items.Add(new ListItem("6PM"));
drpstart.Items.Add(new ListItem("7PM"));
drpstart.Items.Add(new ListItem("8PM"));
drpstart.Items.Add(new ListItem("9PM"));
drpstart.Items.Add(new ListItem("10PM"));
drpstart.Width = 150;
pnlInfo.Controls.Add(drpstart);


DropDownList drpend = new DropDownList();
drpend.ID = "d2"+j;
drpend.Items.Add(new ListItem("8AM"));
drpend.Items.Add(new ListItem("9AM"));
drpend.Items.Add(new ListItem("10AM"));
drpend.Width = 150;
pnlInfo.Controls.Add(drpend);


TextBox txtdesc = new TextBox();
txtdesc.ID = "t3"+j;
txtdesc.Width = 700;
pnlInfo.Controls.Add(txtdesc);

}
i++;
}


this is save button

protected void Button2_Click(object sender, EventArgs e)
{
String str = string.Empty;
DropDownList drpstrattime = (DropDownList)pnlInfo.FindControl("d1");
DropDownList drpend = (DropDownList)pnlInfo.FindControl("d2");
TextBox txtdesc = (TextBox)pnlInfo.FindControl("t3");

string constr = ConfigurationManager.ConnectionStrings["my"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO t2(fromtime,totime,description) VALUES(@fromtime,@totime,@description)"))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@fromtime",drpstrattime.SelectedItem.Value );
cmd.Parameters.AddWithValue("@totime",drpend.SelectedItem.Value);
cmd.Parameters.AddWithValue("@description",txtdesc.Text);
cmd.ExecuteNonQuery();
con.Open();
cmd.ExecuteNonQuery();
Label1.Text = "succes..";
con.Close();
}
}

}



please help me for how to store two dropdown list and one text box value in database
Posted
Comments
Gautham Prabhu K 17-Nov-15 7:10am    
What is the going wrong? any error message you can share?
Member 12097108 17-Nov-15 8:04am    
sir this is not my problem.I solve it again i did but it is not work

From first inspection it looks like you executing query twice.
First time even before connection is opened to DB.

I think connection must be opened first the assigned to SqlCommand.

C#
cmd.ExecuteNonQuery();
con.Open();
cmd.ExecuteNonQuery();
 
Share this answer
 
Please try putting a try{} catch(Exception ex) {} BLock around your SQL query.

like

C#
try{
    cmd.Connection = con;
    cmd.Parameters.AddWithValue("@fromtime",drpstrattime.SelectedItem.Value );
    cmd.Parameters.AddWithValue("@totime",drpend.SelectedItem.Value);
    cmd.Parameters.AddWithValue("@description",txtdesc.Text);
    cmd.ExecuteNonQuery();
    con.Open();
    cmd.ExecuteNonQuery();
    Label1.Text = "succes..";
    con.Close();
}
catch(Exception ex)
{
//Let the exeption be shown (use a message box or whatever :) )
}


then you can find the error way easier.
2nd thing is that i guess your program fails while the first executeQuery().

You have to open the connection before executing!
Also you will get a long back if it worked, i guess a not 0 is it worked.
then you can set the label to sucess if it worked and to false if it failed.(do this in the catch Block)

Debug your code and see if it jumps into the catch block, then please hand us the exeptionmessage.

What are you supposed to do with this?

C#
string constr = ConfigurationManager.ConnectionStrings["my"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO t2(fromtime,totime,description) VALUES(@fromtime,@totime,@description)"))
{


would you mind commenting that out and just do it without the "using"
like this :

C#
string constr = ConfigurationManager.ConnectionStrings["my"].ConnectionString;
SqlConnection con = new SqlConnection(constr);

SqlCommand cmd = new SqlCommand("INSERT INTO t2(fromtime,totime,description) VALUES(@fromtime,@totime,@description)");


Just for a try.
 
Share this answer
 
v2
Comments
Member 12097108 17-Nov-15 8:04am    
sir also i did that but i cant store this value in database
HobbyProggy 17-Nov-15 8:12am    
Did you get an exception message ?
Also, do check if your connection String is correct :)

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