Click here to Skip to main content
11,645,889 members (67,019 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 = "Insert INTO info (day,event) Values (@day,@event)";
        string bag_str = WebConfigurationManager.ConnectionStrings["asgdb01ConnectionString"].ConnectionString;
        conn = new SqlConnection(bag_str);
        conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = strSQL;
        cmd.Parameters.Add
        cmd.Parameters.Add(new SqlParameter("@day", TextBox1.Text));
        cmd.Parameters.Add(new SqlParameter("@event", TextBox2.Text));
 
        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) DAY : ______________ (TEXTBOX1)
(label2) EVENT : ______________ (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 ?

Waiting your helps with four eye.
Thanks.

Did you understand what i mean my friends ? Thanks.

Edit 1 :
Thank you for your answers my friends, but here is the problem ;

The answer is nearly found.Thanks for your answer. But here is the problem. This commands adds new rows into my table, i need to insert an existing row.
 
Here something to make your mind clear;
 
i have penalties table which includes that columuns ;
 
[ID],[NAME],[TYPE],[MON],[TUE],[WED],[THU],[FRI],[SAT],[SUN],[TOTAL],[DAY],[P1],[P2],[OVER].
 
Includes ;
 
ID  NAME  TYPE  MON  TUE....SUN  TOTAL  DAY   EVENT   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 day,p1,p2 and over values must be inserted into that chosen employee.
 
waiting your answer please, i need it too badly..
 
thanks.
Posted 31-Dec-12 13:41pm
Kuthay374
Edited 1-Jan-13 5:16am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you must also have another field in your insert query with the name of employeeID or employeeName.

In such case you need to change your code to the following

protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection conn;
        SqlCommand cmd = new SqlCommand();
        string strSQL = "Insert INTO info (day,event,employee) Values (@day,@event,@employee)";
        string bag_str = WebConfigurationManager.ConnectionStrings["asgdb01ConnectionString"].ConnectionString;
        conn = new SqlConnection(bag_str);
        conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = strSQL;
        cmd.Parameters.Add
        cmd.Parameters.Add(new SqlParameter("@day", TextBox1.Text));
        cmd.Parameters.Add(new SqlParameter("@event", TextBox2.Text));
        cmd.Parameters.Add(new SqlParameter("@employee", Dropdownlist1.selectedItem.Text));
 
        int i = cmd.ExecuteNonQuery();
        conn.Close();
        if (i > 0)
            Response.Write("Data Inserted");
        else
            Response.Write("Try Again");
 
    }

if you think the above is not a correct solution, then post your database design as well here.
  Permalink  
Comments
Kuthay at 1-Jan-13 10:41am
   
can you check my edit again please my friend. thank you for answer.
Kuthay at 1-Jan-13 11:02am
   
But when i use these codes, the button click event adds new rows columns into my database with the new values. Does not changes the existing employee's values.
It must change the chosen employee's data's. Could you understand it my friend ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You are doing everything in a right way..for your question you only have to pass one more parameter for selectedvalue of dropdownlist.

cmd.Parameters.Add(new SqlParameter("@day", TextBox1.Text));
        cmd.Parameters.Add(new SqlParameter("@event", TextBox2.Text));
cmd.Parameters.Add(new SqlParameter("@employee", dropdownlist1.SelectedValue));


Thanks
  Permalink  
Comments
Kuthay at 1-Jan-13 10:42am
   
thanks for your answer but can you can my edit update my friend.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,

First of all you should add Event column in the table.
As per my advice and your scenario i think you should use the update command. Because we have ID in the table as a Primary key. So whenever you are binding the dropdownlist from the table just do like below

DropDownList1.DataTextField="Name"
DropDownList1.DataValueField="ID"

And then you can use the update command instead of Insert command as shown below

string strSQL = "UPDATE TABLE info SET day=@day,event=@event where ID=Convert.ToInt32(DropDownList1.SelectedValue.ToString())";

Please check this and revert back to me in case you need any further clarification.

Thanks and Regards
Suman Zalodiya
  Permalink  
v2

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

  Print Answers RSS
0 jyo.net 814
1 OriginalGriff 648
2 Sergey Alexandrovich Kryukov 478
3 CPallini 414
4 Afzaal Ahmad Zeeshan 378
0 OriginalGriff 1,143
1 jyo.net 994
2 Sergey Alexandrovich Kryukov 808
3 Afzaal Ahmad Zeeshan 702
4 DamithSL 691


Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 17 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100