页面建立了几个Texbox,几个lable,几个按钮,想实现获取Texbox的值,存储到SQL数据库中,代码一直出错未将对象引用设置到对象的实例 <form id="form1" runat="server"> <div style="height: 463px; width: 862px"> <table align="left" style="height: 442px; width: 833px"> <tr valign="top"> <td> <br /> <br /> <br /> <br /> <asp:Label ID="Label1" runat="server" Text="工序号:"></asp:Label> <asp:TextBox ID="TextBox1" runat="server" Height="16px" Width="132px"></asp:TextBox> <br /> <br /> <asp:Label ID="Label2" runat="server" Text="工序名:"></asp:Label> <asp:DropDownList ID="DropDownList1" runat="server" Height="16px" Width="130px"> <asp:ListItem>车</asp:ListItem> <asp:ListItem>铣</asp:ListItem> <asp:ListItem>刨</asp:ListItem> <asp:ListItem>磨</asp:ListItem> <asp:ListItem>钻</asp:ListItem> <asp:ListItem>扩</asp:ListItem> <asp:ListItem>铰</asp:ListItem> </asp:DropDownList> <br /> <br /> <asp:Label ID="Label3" runat="server" Text="工序描述:"></asp:Label> <asp:TextBox ID="TextBox3" runat="server" Height="16px" Width="132px"></asp:TextBox> <br /> <br /> <asp:Label ID="Label4" runat="server" Text="加工车间:"></asp:Label> <asp:TextBox ID="TextBox4" runat="server" Height="16px" Width="132px"></asp:TextBox> <br /> <br /> <asp:Label ID="Label5" runat="server" Text="机床:"></asp:Label> <asp:TextBox ID="TextBox5" runat="server" Height="16px" Width="132px"></asp:TextBox> <br /> <br /> <asp:Label ID="Label6" runat="server" Text="刀具:"></asp:Label> <asp:TextBox ID="TextBox6" runat="server" Height="16px" Width="132px"></asp:TextBox> <br /> <br /> <asp:Label ID="Label7" runat="server" Text="夹具:"></asp:Label> <asp:TextBox ID="TextBox7" runat="server" Height="16px" Width="132px"></asp:TextBox> <br /> <br /> <br /> <asp:Button ID="Button2" runat="server" Text="添加" onclick="Button2_Click" /> <asp:Button ID="Button3" runat="server" Text="修改" /> <asp:Button ID="Button1" runat="server" Text="返回" PostBackUrl="~/Manufacturing Method/newfound.aspx" /> </td> </tr> </table> </div> </form> 后面的连接数据库 protected void Button2_Click(object sender, EventArgs e) { SqlDataAdapter da = new SqlDataAdapter(); //创建连接对象 SqlConnection conn = new SqlConnection("Data Source=BUAA-TORNADO;Initial Catalog=knowledgebase;Integrated Security=True"); //创建查询命令对象 conn.Open(); SqlCommand selectCmd = new SqlCommand(); selectCmd.CommandText = "select * from Processes"; selectCmd.Connection = conn; //创建添加数据的命令对象 SqlCommand insertCmd = new SqlCommand(); insertCmd.CommandText = "insert into Processes values(@ProcessesID,@ProcessesDescribe,@Workshop,@Machine,@Tool)"; insertCmd.Connection = conn; //向插入命令添加参数 insertCmd.Parameters.Add("@ProcessesID",SqlDbType.NVarChar,50, "ProcessesID"); insertCmd.Parameters.Add("@ProcessesDescribe",SqlDbType.NVarChar,50, "ProcessesDescribe"); insertCmd.Parameters.Add("@Workshop", SqlDbType.NVarChar, 8, "Workshop"); insertCmd.Parameters.Add("@Machine", SqlDbType.NVarChar, 8, "Machine"); insertCmd.Parameters.Add("@Tool", SqlDbType.NVarChar, 50, "Tool"); da.SelectCommand = selectCmd; da.InsertCommand = insertCmd; //创建数据集对象 DataSet data = new DataSet(); //使用数据适配器填充数据适配器 da.Fill(data, " Processes"); DataRow drNew=data.Tables["Processes"].NewRow(); //设置新添加行的值 drNew[0] = TextBox1.Text; //drNew["ProcessesName"] = TextBox3.Text; drNew[1] = TextBox3.Text; drNew[2] = TextBox4.Text; drNew[3] = TextBox5.Text; drNew[4] = TextBox6.Text; //向表中添加行 data.Tables["Processes"].Rows.Add(drNew); //将数据通过数据适配器更新到数据库中 da.Update(data, "Processes"); } 运行错误: 请各位大神帮满看一下,我是初学者,感激不尽谢谢了 |
|
10分 |
" Processes" Tables["Processes"] 一个有空格,一个没空格 |
10分 |
1#正解
或者你的代码改成 DataRow drNew=data.Tables[0].NewRow(); |
你的表有7个字段,但你的insert却只有5个值
你应该改成下面这种格式,减少不必要的异常 insert into table(c1,c2) value(@c1,@c2) |
|
具体是这样么? insertCmd.CommandText = "insert into table Processes values(@ProcessesID,@ProcessesDescribe,@Workshop,@Machine,@Tool,@Clamp)"; 但是还是有问题不能运行,因为我第二个想用Dropdownlist, 所以没办法填满。那么我想要实现“获取TEXTBOX和dropdownlist”里面的数据,存到SQL表中,应该怎么做呢?谢谢了。 |
|
insertCmd.Parameters.Add("@ProcessesName",SqlDbType.NVarChar,50, dropDownList.SelectValue); 类似这样传递参数 |
|
跪了。。。我的代码 protected void Button2_Click(object sender, EventArgs e) { SqlDataAdapter da = new SqlDataAdapter(); //创建连接对象 SqlConnection conn = new SqlConnection("Data Source=BUAA-TORNADO;Initial Catalog=knowledgebase;Integrated Security=True"); //创建查询命令对象 conn.Open(); SqlCommand selectCmd = new SqlCommand(); selectCmd.CommandText = "select * from Processes"; selectCmd.Connection = conn; //创建添加数据的命令对象 SqlCommand insertCmd = new SqlCommand(); insertCmd.CommandText = "insert into table Processes values(@ProcessesID,@ProcessesDescribe,@Workshop,@Machine,@Tool,@Clamp)"; insertCmd.Connection = conn; //向插入命令添加参数 insertCmd.Parameters.Add("@ProcessesID",SqlDbType.VarChar,50, "ProcessesID"); insertCmd.Parameters.Add ("@ProcessesName", SqlDbType.VarChar, 50, DropDownList1.SelectedValue ); insertCmd.Parameters.Add("@ProcessesDescribe",SqlDbType.VarChar,50, "ProcessesDescribe"); insertCmd.Parameters.Add("@Workshop", SqlDbType.VarChar, 50, "Workshop"); insertCmd.Parameters.Add("@Machine", SqlDbType.VarChar, 50, "Machine"); insertCmd.Parameters.Add("@Tool", SqlDbType.VarChar, 50, "Tool"); insertCmd.Parameters.Add("@Clamp", SqlDbType.VarChar, 50, "Clamp"); da.SelectCommand = selectCmd; da.InsertCommand = insertCmd; //创建数据集对象 DataSet data = new DataSet(); //使用数据适配器填充数据适配器 da.Fill(data, "Processes"); //向DataSet的“Books”表中添加一条记录 DataRow drNew=data.Tables["Processes"].NewRow(); //设置新添加行的值 drNew[0] = TextBox1.Text; drNew[1] = DropDownList1.SelectedItem.Text; drNew[2] = TextBox3.Text; drNew[3] = TextBox4.Text; drNew[4] = TextBox5.Text; drNew[5] = TextBox6.Text; drNew[6] = TextBox7.Text; //向表中添加行 data.Tables["Processes"].Rows.Add(drNew); //将数据通过数据适配器更新到数据库中 da.Update(data, "Processes"); } 然后错误是 我可能里面的东西写的不对,因为我是自学的。谢谢了,再帮我看一下吧 |
|
你这是Insert不是update
|
|
能说的具体一点么?上面的代码关于DROPDOWNLIST不对的地方,我现在基础不太好,感激不尽了。 |
|
我能加你QQ么、?谢谢谢谢 我Q1332945110 |
|
我又换了一个方法~结贴
|