使用下拉列表条件插入数据 [英] Insert Data with Dropdownlist Condition

查看:74
本文介绍了使用下拉列表条件插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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");

    }





这是你要清楚的事情。



| Dropdownlist1 | (我会点击这里,你会知道名字。)

-Mike

-John

-Susan

-Kevin



(label1)DAY:______________(TEXTBOX1)

(label2)活动:______________(TEXTBOX2)

|完成| (按钮1)



从这里看,我将点击下拉列表,我将选择该员工。在我输入值后,我将点击FINISH按钮,数据将被插入到所选员工中。



这是关键问题;我怎么能告诉button_click事件;您将这些数据插入到选定的员工表中,我从下拉列表中选择了该员工。好的 ?



用四只眼睛等待你的帮助。

谢谢。



你明白我的意思是什么?谢谢。



编辑1:

感谢您的回答我的朋友们,但这是问题所在;





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.

推荐答案

你还必须有另一个字段在您的插入查询中,名称为employeeID或employeeName。



在这种情况下,您需要将代码更改为以下内容



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.


你正在以正确的方式做所有事情。对于你的问题,你只需要为下拉列表的选定值传递一个参数。



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


您好,



首先,您应该在表格中添加Event列。

根据我的建议和你的场景我认为你应该使用更新命令。因为我们在表中将ID作为主键。因此,无论何时绑定表中的下拉列表,只需执行以下操作



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"





然后您可以使用update命令而不是Insert命令,如下所示





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())";





如果您需要进一步说明,请查看此产品并退回给我。



谢谢和问候

Suman Zalodiya



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

Thanks and Regards
Suman Zalodiya


这篇关于使用下拉列表条件插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆