在C#中找不到表0错误

在C#中找不到表0错误

本文介绍了在C#中找不到表0错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试执行存储过程并获取空数据集时收到此错误。

I get This error when i try to execute stored procedure and get null dataset.

private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["learnaspConnectionString"].ConnectionString);

protected void btnregister_Click(object sender, EventArgs e)
{
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("dbo.RegisterInsert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("@fname", txtfname.Text);
            SqlParameter p2 = new SqlParameter("@lname", txtlname.Text);
            SqlParameter p3 = new SqlParameter("@company", txtcompany.Text);
            SqlParameter p4 = new SqlParameter("@phone", txtphone.Text);
            SqlParameter p5 = new SqlParameter("@address", txtaddress.Text);
            SqlParameter p6 = new SqlParameter("@email", txtemail.Text);
            cmd.Parameters.Add(p1);
            cmd.Parameters.Add(p2);
            cmd.Parameters.Add(p3);
            cmd.Parameters.Add(p4);
            cmd.Parameters.Add(p5);
            cmd.Parameters.Add(p6);
            cmd.ExecuteNonQuery();

            con.Close();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            ListViewDetails.DataSource = ds;
            ListViewDetails.DataBind();
            lblmessage.Visible = true;
            lblmessage.Text = "Registration Completed Successfully!";
        }
        catch (SqlException ex)
        {
        }
}

我的存储过程为:

   CREATE PROCEDURE [dbo].[RegisterInsert]
    -- Add the parameters for the stored procedure here
    @fname nvarchar(50),
    @lname nvarchar(50),
    @company nvarchar(50),
    @phone nvarchar(50),
    @address nvarchar(250),
    @email  nvarchar(250)
       SET NOCOUNT ON;
        insert into [dbo].[register]
    ([fname],
     [lname],
     [company],
     [phone],
     [address],
     [email])
     values
     (@fname,
      @lname,
      @company,
      @phone,
      @address,
      @email)
END

我无法获取当我运行我的代码时,在Listview控件中显示数据。按钮单击事件两次在数据库中存储值,并给出错误找不到表0。

I am not able to get display data in Listview control when i run my code.Button Click event stores values in Database twice and gives error'Cannot find table 0'.

请帮助我知道我的代码在哪里错了。

Please help me to let me know where i am wrong in my code.

预先感谢。

推荐答案

问题是数据集不包含任何表对象。因此,DS.Table [0]不存在,将引发错误。
添加检查以确保数据集在继续之前具有表。

The problem is the Dataset does not contain any table objects. Therefore DS.Table[0] does not exist and will thrown an error.Add a check to ensure the dataset has tables before proceeding.

    // check if ds has a table
    if(DS.TABLES.COUNT>0)
    {
       //check if ds.table[0] has any rows
       if(DS.TABLES[0].ROWS.COUNT>0){
       //do something
    }
 }

这篇关于在C#中找不到表0错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 13:26