i have asp.net project which has dropdownlist. i need to insert data into my database according to dropdownlist condition.
For example; in my project, i will choose the employee and i will insert the some values from my textboxes and after clicking button on the bottom, i need to insert the changes into database that selected employee from my dropdownlist.
Here is the button click codes;
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand cmd = new SqlCommand();
string strSQL = "UPDATE info (empid,day1,day2,date1) Values (@empid,@day1,@day2,@date1)";
string bag_str = WebConfigurationManager.ConnectionStrings["asgdb01ConnectionString"].ConnectionString;
conn = new SqlConnection(bag_str);
conn.Open();
cmd.Connection = conn;
cmd.CommandText = strSQL;
cmd.Parameters.Add(new SqlParameter("@day1", Int32.Parse(DropDownList1.SelectedItem.Value)));
cmd.Parameters.Add(new SqlParameter("@day1", Int32.Parse(TextBox1.Text)));
cmd.Parameters.Add(new SqlParameter("@day2", Int32.Parse(TextBox2.Text)));
cmd.Parameters.Add(new SqlParameter("@date1", CalendarExtender1.SelectedDate));
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
Response.Write("Data Inserted");
else
Response.Write("Try Again");
}
Here is something to make clear on your mind;
|Dropdownlist1| (i will click here and names will appear you know.)
-Mike
-John
-Susan
-Kevin
(label1) DAY1 : ______ (TEXTBOX1)
(label2) DAY2: ______ (TEXTBOX2)
|FINISH| (button1)
You see from here, i will click dropdownlist and i will choose the employee. After i will enter the values and i will click on FINISH button and data will be inserted into that selected employee.
Here is the key question; how can i tell the button_click event; you will insert these data’s into selected employees table which i have chosen the employee from dropdownlist. okay ?
I tried to make sample of the table here;
ID NAME TYPE MON TUE …. SUN TOTAL DAY1 DAY2 DATE
1 mike out 250 350 ….. 0 900 – – 22-06-2012
2 john in 350 150 …. 100 1100 – – 28-06-2012
As you see from here my friend, i will choose the dates from my datetimepickers, i will choose the employee from my dropdownlist and i will enter the values into textboxes and after that i will click finish button.
the day1,day2 values must be inserted into that chosen employee.
waiting your answer please, i need it too badly..
thanks.
Waiting your helps with four eye.
Thanks.
Did you understand what i mean my friends ? Thanks.
If you did not set the dropdownlist’s
SelectedValue, you should set it to be the employee’s id.Then, modify your function to also take into account the value in the dropdownlist like this:
Notice the change is using the
SelectedValueproperty of the DropdownList to find the correct Id