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

.Net技术 码拜 9年前 (2015-05-10) 1169次浏览 0个评论

页面建立了几个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 />
        &nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label1" runat="server" Text="工序号:"></asp:Label>
        &nbsp;

        <asp:TextBox ID="TextBox1" runat="server" Height="16px" Width="132px"></asp:TextBox>
        <br />
        <br /> &nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label2" runat="server" Text="工序名:"></asp:Label>
        &nbsp;
        <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 />&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label3" runat="server" Text="工序描述:"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server" Height="16px" Width="132px"></asp:TextBox>
        <br />
        <br /> &nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label4" runat="server" Text="加工车间:"></asp:Label>
        <asp:TextBox ID="TextBox4" runat="server" Height="16px" Width="132px"></asp:TextBox>
        <br />
        <br /> &nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label5" runat="server" Text="机床:"></asp:Label>
        &nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox5" runat="server" Height="16px" Width="132px"></asp:TextBox>
        <br />
        <br />&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label6" runat="server" Text="刀具:"></asp:Label>
        &nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox6" runat="server" Height="16px" Width="132px"></asp:TextBox>
        <br />
        <br />&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label7" runat="server" Text="夹具:"></asp:Label>
        &nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox7" runat="server" Height="16px" Width="132px"></asp:TextBox>
        <br />
        <br />
        <br />
   
        &nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="Button2" runat="server" Text="添加" onclick="Button2_Click"  
            
            />&nbsp;&nbsp;&nbsp;&nbsp; 

                <asp:Button ID="Button3" runat="server" Text="修改"  
            
            />
    &nbsp;&nbsp;&nbsp;&nbsp; 
    
    <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"); 
        
    }

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

请各位大神帮满看一下,我是初学者,感激不尽谢谢了

10分
" Processes"
Tables["Processes"]

一个有空格,一个没空格

10分
1#正解
或者你的代码改成

DataRow drNew=data.Tables[0].NewRow();
引用 1 楼 starfd 的回复:
" Processes"
Tables["Processes"]

一个有空格,一个没空格

哇,真帮我大忙了,检查一天了。不过我运行又出现问题了。
C#中向TexBox里面添加数据,并保存在SQL数据库中出错。未将对象引用设置到对象的实例

这是为什么呀
其中SQL表如图
C#中向TexBox里面添加数据,并保存在SQL数据库中出错。未将对象引用设置到对象的实例

第二行的ProcessesName我想用dropdownlist,所以就没写,请问是由于什么原因出错了呢

你的表有7个字段,但你的insert却只有5个值
你应该改成下面这种格式,减少不必要的异常

insert into table(c1,c2) value(@c1,@c2)
引用 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表中,应该怎么做呢?谢谢了。
C#中向TexBox里面添加数据,并保存在SQL数据库中出错。未将对象引用设置到对象的实例

insertCmd.Parameters.Add("@ProcessesName",SqlDbType.NVarChar,50, dropDownList.SelectValue);

类似这样传递参数

引用 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"); 
        
    }

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

我可能里面的东西写的不对,因为我是自学的。谢谢了,再帮我看一下吧

你这是Insert不是update
引用 8 楼 lovelj2012 的回复:

你这是Insert不是update

能说的具体一点么?上面的代码关于DROPDOWNLIST不对的地方,我现在基础不太好,感激不尽了。

引用 8 楼 lovelj2012 的回复:

你这是Insert不是update

我能加你QQ么、?谢谢谢谢 我Q1332945110

我又换了一个方法~结贴

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#中向TexBox里面添加数据,并保存在SQL数据库中出错。未将对象引用设置到对象的实例
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!