C#中向TexBox里头添加数据,并保存在SQL数据库中出错。未将对象引用设置到对象的实例
C#中向TexBox里面添加数据,并保存在SQL数据库中出错。未将对象引用设置到对象的实例
页面建立了几个Texbox,几个lable,几个按钮,想实现获取Texbox的值,存储到SQL数据库中,代码一直出错未将对象引用设置到对象的实例
代码:
后面的连接数据库
运行错误:
![C#中向TexBox里头添加数据,并保存在SQL数据库中出错。未将对象引用设置到对象的实例 C#中向TexBox里头添加数据,并保存在SQL数据库中出错。未将对象引用设置到对象的实例](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDE1LzA1LzAxLzExNDczNDcwLnBuZw==)
请各位大神帮满看一下,我是初学者,感激不尽谢谢了
------解决思路----------------------
一个有空格,一个没空格
------解决思路----------------------
1#正解
或者你的代码改成
页面建立了几个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");
}
运行错误:
请各位大神帮满看一下,我是初学者,感激不尽谢谢了
------解决思路----------------------
" Processes"
Tables["Processes"]
一个有空格,一个没空格
------解决思路----------------------
1#正解
或者你的代码改成
DataRow drNew=data.Tables[0].NewRow();