错误-使用“新建" keword创建对象,对象引用未设置为实例 [英] Error - Use the "New" keword to create the object, Object Reference not set to an instance

查看:86
本文介绍了错误-使用“新建" keword创建对象,对象引用未设置为实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误为-1)使用新建"关键字创建对象
2)对象引用未设置为实例

该行出现错误-

Errors are - 1 ) Use the "New" keword to create the object
2) Object Reference not set to an instance

Error is giving in this line -

rs = ds.Tables["registration"].Select("email='" + sa + "'");



我在CS中的代码是




My code in cs is


protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection CON = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["tuitionConnectionString"].ConnectionString);
        CON.Open();
        ds = new DataSet();
        string sql = "select *  from registration";
        da = new SqlDataAdapter(sql, CON);
        da1 = new SqlDataAdapter(sql, CON);
        string sa = txtEmail.Text;
        string pw = txtPass.Text;
        int r = 0, r1 = 0;
        DataRow[] rs;
        DataRow[] rs1;
        rs = ds.Tables["registration"].Select("email=''" + sa + "''");
        r = rs.Length;
        
        rs1 = ds.Tables["registration"].Select("pass=''" + pw + "''");
        r1 = rs1.Length;
        
        if (r!=0 && r1!=0)
        {
            txtEmail.Text = r.ToString();
            txtPass.Text = r1.ToString();
          
        }
        else
        {
            System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
            lbl1.ForeColor = System.Drawing.Color.Yellow;
            lbl1.BackColor = System.Drawing.Color.Blue;
            lbl1.Text = "Please Enter a Valid User ID and Password";
            ph1.Controls.Add(lbl1);
        }
        CON.Close();
    }



[edit]已添加代码块,将我的内容作为纯文本..."选项已禁用-OriginalGriff [/edit]



[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]

推荐答案

它看起来像ds.Tables["registration"]返回无效的.您可能已经拼写了这个硬编码的字符串索引或类似的东西.没有大碍.始终使用调试器来避免此类问题.顺便提一下这样的问题.

使用" new"…"是一个虚假的建议.有时您需要使用新",有时则不需要.

—SA
It looks like ds.Tables["registration"] returns null. You could have misspelled this hard-coded string index or something like that. Not a big deal. Always use the Debugger to avoid such problems. And such questions, by the way.

"Use ''new''…" is a bogus suggestion. Sometime you need to use "new", sometimes not.

—SA


查看您的代码:

Look at your code:

ds = new DataSet();
string sql = "select *  from registration";
da = new SqlDataAdapter(sql, CON);
da1 = new SqlDataAdapter(sql, CON);
string sa = txtEmail.Text;
string pw = txtPass.Text;
int r = 0, r1 = 0;
DataRow[] rs;
DataRow[] rs1;
rs = ds.Tables["registration"].Select("email='" + sa + "'");

以任何方式排除所有不引用(因而影响)ds的行:

Taking out all the lines which do not refer to (and thus affect) ds in any way:

ds = new DataSet();
rs = ds.Tables["registration"].Select("email=''" + sa + "''");

这意味着您正在创建一个新的空DataSet,然后尝试使用其中的表.它为空,因此没有表,因此会出现对象引用..."错误.
我怀疑在尝试使用DataSet之前,您需要da或da1中的SqlDataAdapter.Fill(ds).
我不知道为什么会有两个相同的SqlDataAdapter,但是...

Which means that you are ctreateing a new, empty DataSet, and then trying to use a table from it. It is empty, so no tables, so you get the "object Reference..." error.
I suspect that you need an SqlDataAdapter.Fill(ds) from either da or da1 before you try to use the DataSet.
I don''t know why you have two identical SqlDataAdapters, though...


您需要查看^ ] .

使用数据集上数据适配器的填充方法.

You need to look at how to populate a Dataset[^].

Use the fill method of the Data Adapter on the Dataset.
Something like
da.fill(ds,"registration");


这篇关于错误-使用“新建" keword创建对象,对象引用未设置为实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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