如何使用数据表和存储过程在数据库中存储数据? [英] How to store data in database using datatable and stored procedures?

查看:85
本文介绍了如何使用数据表和存储过程在数据库中存储数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下代码的NullReferenceException。它应该使用存储过程获取值并将它们存储在数据库中。创建和存储数据表的方法对于其他表工作正常,并且几天前也在为此表工作,但现在只针对该表,它抛出异常'对象引用未设置为对象的实例'。我哪里错了?





I'm getting the NullReferenceException for the following code. It is supposed to take values and store them in the database using a stored procedure. The method for creating and storing the datatable is working fine for other tables and was also working for this table too a couple of days ago but now only for this table it throws the exception 'Object reference not set to an instance of an object'. Where am I going wrong??


public DataTable SelectStoreProcedure_DataTable(string[] _parnames, string[] _parvalues, string _procname)
    {
        _dt = new DataTable();
        _con = new SqlConnection(_strcon);
        _cmd = new SqlCommand();

        try
        {

            _con.Open();
            for (int i = 0; i < _parnames.Length; i++)
            {
                _cmd.Parameters.AddWithValue(_parnames[i], _parvalues[i].Replace("'",""));
            }
            _cmd.CommandText = _procname;
            _cmd.CommandType = CommandType.StoredProcedure;
            _cmd.Connection = _con;
            _da = new SqlDataAdapter(_cmd);
            _da.Fill(_dt);
            _con.Close();
            _cmd.Parameters.Clear();

        }
        catch (Exception ex)
        {
            _con.Close();
        }
        return _dt;
    }







protected void btnsave_Click(object sender, EventArgs e)
        {
            string[] parnames = new string[19];
            string[] parvalues = new string[19];

            parnames[0] = "@DistrictID";
            parnames[1] = "@TehsilID";
            parnames[2] = "@PoliceStationID";
            parnames[3] = "@Category";
            parnames[4] = "@CNIC";
            parnames[5] = "@Name";
            parnames[6] = "@Alias";
            parnames[7] = "@Parentage";
            parnames[8] = "@Caste";
            parnames[9] = "@TemporaryAddress";
            parnames[10] = "@PermanentAddress";
            parnames[11] = "@Age";
            parnames[12] = "@YearofBirth";
            parnames[13] = "@Education";
            parnames[14] = "@Languages";
            parnames[15] = "@Speech";
            parnames[16] = "@LiveliHood";
            parnames[17] = "@Property";
            parnames[18] = "@FilePath";
            parvalues[0] = ddlDistrict.SelectedValue;
            parvalues[1] = ddlTehsil.SelectedValue;
            parvalues[2] = ddlPoliceStation.SelectedValue;
            parvalues[3] = txtCategory.Text;
            parvalues[4] = txtCNIC.Text;
            parvalues[5] = txtName.Text;
            parvalues[6] = txtAlias.Text;
            parvalues[7] = txtPercentage.Text;
            parvalues[8] = txtCaste.Text;
            parvalues[9] = txtPAddress.Text;
            parvalues[10] = txtTAddress.Text;
            parvalues[11] = txtAge.Text;
            parvalues[12] = txtBirth.Text;
            parvalues[13] = txtEducation.Text;
            parvalues[14] = txtLanguage.Text;
            parvalues[15] = txtSpeech.Text;
            parvalues[16] = txtLivingHood.Text;
            parvalues[17] = txtProperty.Text;
            parvalues[18] = image;


            DataTable dataTable = dbManager.SelectStoreProcedure_DataTable(parnames, parvalues, "AddBioData");
            Session["BioDataID"] = dataTable.Rows[0]["BioDataID"];

            Response.Redirect("Convication.aspx", false);

            Session["DistrictID"] = ddlDistrict.Text;
            Session["TehsilID"] = ddlTehsil.Text;
        }

推荐答案

我们无法分辨 - 我们可以运行你的代码,无论如何都无法访问你的数据或数据库。



所以你必须自己找。

从调试器开始。

在该方法的第一行放置一个断点,并在执行每一行之前开始查看数据。



我猜_parvalues [18]为空 - 但只有你可以检查一下。即使不是,你需要仔细看看它是什么。如果它是一个图像类的实例,那么无论如何都不会工作 - 它会在你的数据库中插入类似System.Drawing.Bitmap的东西。请参阅此处:为什么我会得到一个参数无效。我从数据库中读取图像时出现异常? [ ^ ]



当你知道哪一行包含空值时,你可以开始查看原因 - 而且可能不在那段代码中!
We can't tell - we can;t run your code, and don;t have access to your data or database anyway.

So you will have to find out for yourself.
Start with the debugger.
Put a breakpoint on the first line of that method, and start looking at teh data before you execute each line.

I'm guessing that _parvalues[18] is null - but only you can check that. And even if it isn't, you need to look at what it is fairly carefully. If it's an instance of teh Image class, that won't work anyway - it'll insert something like "System.Drawing.Bitmap" into your DB. See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]

When you know which line contains the null value, you can start looking at why - and it's probably not in that code!


这篇关于如何使用数据表和存储过程在数据库中存储数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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