我在此代码中有问题.请 [英] I have a problem in this code .please

查看:88
本文介绍了我在此代码中有问题.请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我是初学者.我的代码需要帮助.在我的按钮中,在datagrid视图中添加新记录后,出现以下错误消息:
对象引用未设置为对象的实例

这是我的代码:

Hi everyone,
I''m a beginner. I need help with my code. In my button, I get this error message after adding a new record in datagrid view:
object reference not set to an instance of an object

Here is my code:

con = new SqlConnection(dbconnection.constring);
da.InsertCommand = new SqlCommand("INSERT INTO item VALUES (@item_name,@price)", con);
da.InsertCommand.Parameters.Add("@item_name", SqlDbType.NVarChar).Value = textBox1.Text;
da.InsertCommand.Parameters.Add("@price", SqlDbType.Money).Value = textBox2.Text;
con.Open();
da.InsertCommand.ExecuteNonQuery();
con.Close();


推荐答案

您好.
您可以再发布一些代码吗?
可能未初始化"da"或"dbconnection.constring".

Hello.
can you post a little bit more code?
probably "da" or "dbconnection.constring" are not initialized.

SqlDataAdapter da = new SqlDataAdapter();
SqlConnection con = new SqlConnection(dbconnection.constring);
da.InsertCommand = new SqlCommand("INSERT INTO item VALUES (@item_name,@price)", con);
da.InsertCommand.Parameters.Add("@item_name", SqlDbType.NVarChar).Value = textBox1.Text;
da.InsertCommand.Parameters.Add("@price", SqlDbType.Money).Value = textBox2.Text;
con.Open();
da.InsertCommand.ExecuteNonQuery();
con.Close();



尝试检查此链接: http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldataadapter.insertcommand.aspx [



try to check this link: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.insertcommand.aspx[^]

here you can find more useful information

Regards
Robert


这有点题外话,但是为什么要使用数据适配器呢?根据显示的代码,您可以创建命令,设置参数并执行命令.适配器不是必需的.因此,如果您只需要从几个文本框中插入一行,而在代码中不使用任何其他适配器功能,则可以将其简化为:
This is a bit off-topic, but why are you using the data adapter at all. As far as the code is shown, you create a command, set the parameters and execute the command. Adapter isn''t necessary for this. So if you simply need to insert a row from few text boxes and you don''t use any other adapter functionality in your code, you could possibly simplify this to:
con = new SqlConnection(dbconnection.constring);
SqlCommand cmd = new SqlCommand("INSERT INTO item VALUES (@item_name,@price)", con);
cmd.Parameters.Add("@item_name", SqlDbType.NVarChar).Value = textBox1.Text;
cmd.Parameters.Add("@price", SqlDbType.Money).Value = textBox2.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();


下面的代码对我有用.

Below code worked for me .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;   
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Sql;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            SqlConnection con = new SqlConnection("user id=dbuser;" +
                                       "password=pass@123;server=10.1.2.21;" +
                                       "Trusted_Connection=yes;" +
                                       "database=TestDB; " +
                                       "connection timeout=30");          
            SqlCommand cmd = new SqlCommand("INSERT INTO item VALUES (@item_name,@price)", con);
            cmd.Parameters.Add("@item_name", SqlDbType.NVarChar).Value = '1';
            cmd.Parameters.Add("@price", SqlDbType.Money).Value = 100;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

        }
    }
}


这篇关于我在此代码中有问题.请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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