如何在c#中的sqlserver表中存储dyanamic下拉列表和文本框数据 [英] How to store dyanamic dropdownlist and textbox data in sqlserver table in c#

查看:74
本文介绍了如何在c#中的sqlserver表中存储dyanamic下拉列表和文本框数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好先生,

我有两个动态下拉列表和一个文本框。现在我想将这些数据存储到sqlserver数据库中。

我已编写代码但是它不能正常工作。

我会发送我的代码请帮帮我。



这是添加按钮(添加新行(下拉列表和文本框)

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。添加(new ListItem(11AM));

drpstart.Items.Add(new ListItem(12PM));

drpstart.Items.Add(new ListItem(1PM));

drpstart.Items.Add(new ListItem(2PM));

drpstart.Items。添加(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); < br $>




TextBox txtdesc = new TextBox();

txtdesc.ID =t3+ j;

txtdesc.Width = 700;

pnlInfo.Controls.Add(txtdesc);



}

i ++;

}





这是保存按钮



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.ConnectionSt ring [my]。ConnectionString;

using(SqlConnection con = new SqlConnection(constr))

{

using(SqlCommand cmd =新的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。值);

cmd.Parameters.AddWithValue(@ description,txtdesc.Text);

cmd.ExecuteNonQuery();

con .Open();

cmd.ExecuteNonQuery();

Label1.Text =succes ..;

con.Close() ;

}

}



}







请帮我解决如何在数据库中存储两个下拉列表和一个文本框值

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

推荐答案

从第一次检查看起来你执行两次查询。

甚至在连接打开DB之前第一次。



我认为连接必须首先打开分配给SqlCommand。



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.

cmd.ExecuteNonQuery();
con.Open();
cmd.ExecuteNonQuery();


请尝试使用try {} catch(Exception ex){} BLock围绕您的SQL查询。 />


喜欢



Please try putting a try{} catch(Exception ex) {} BLock around your SQL query.

like

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 :) )
}





然后你可以更容易地找到错误的方式。

第二件事是我猜你的程序在第一个executeQuery()时失败。



你必须在执行前打开连接!

如果它有效,你会得到一个很长的回来,我想不是0它是否有效。

那么你可以将标签设置成成功,如果失败则设置为false。(在catch块中执行此操作)



调试代码和看看它是否跳进了catch区块,然后请把我们的exeptionmessage给我们。



你应该怎么办?





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?

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 :

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.


这篇关于如何在c#中的sqlserver表中存储dyanamic下拉列表和文本框数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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