C#中向TexBox里面添加数据,并保存在SQL数据库中出错。未将对象引用设置到对象的实例

zt1120110894 2015-04-30 03:15:12
页面建立了几个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");

}


运行错误:


请各位大神帮满看一下,我是初学者,感激不尽谢谢了
...全文
227 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zt1120110894 2015-04-30
  • 打赏
  • 举报
回复
我又换了一个方法~结贴
zt1120110894 2015-04-30
  • 打赏
  • 举报
回复
引用 8 楼 lovelj2012 的回复:
你这是Insert不是update
我能加你QQ么、?谢谢谢谢 我Q1332945110
zt1120110894 2015-04-30
  • 打赏
  • 举报
回复
引用 8 楼 lovelj2012 的回复:
你这是Insert不是update
能说的具体一点么?上面的代码关于DROPDOWNLIST不对的地方,我现在基础不太好,感激不尽了。
江南小鱼 2015-04-30
  • 打赏
  • 举报
回复
你这是Insert不是update
zt1120110894 2015-04-30
  • 打赏
  • 举报
回复
引用 6 楼 starfd 的回复:
insertCmd.Parameters.Add("@ProcessesName",SqlDbType.NVarChar,50, dropDownList.SelectValue);

类似这样传递参数


引用 6 楼 starfd 的回复:
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");

}


然后错误是



我可能里面的东西写的不对,因为我是自学的。谢谢了,再帮我看一下吧
  • 打赏
  • 举报
回复
insertCmd.Parameters.Add("@ProcessesName",SqlDbType.NVarChar,50, dropDownList.SelectValue);
类似这样传递参数
zt1120110894 2015-04-30
  • 打赏
  • 举报
回复
引用 4 楼 starfd 的回复:
你的表有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表中,应该怎么做呢?谢谢了。
  • 打赏
  • 举报
回复
你的表有7个字段,但你的insert却只有5个值 你应该改成下面这种格式,减少不必要的异常
insert into table(c1,c2) value(@c1,@c2)
zt1120110894 2015-04-30
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
" Processes"
Tables["Processes"]

一个有空格,一个没空格


哇,真帮我大忙了,检查一天了。不过我运行又出现问题了。


这是为什么呀
其中SQL表如图


第二行的ProcessesName我想用dropdownlist,所以就没写,请问是由于什么原因出错了呢
江南小鱼 2015-04-30
  • 打赏
  • 举报
回复
1#正解 或者你的代码改成
DataRow drNew=data.Tables[0].NewRow();
  • 打赏
  • 举报
回复
" Processes"
Tables["Processes"]
一个有空格,一个没空格

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧