【问题标题】:System.IndexOutOfRangeException: Cannot find table 0System.IndexOutOfRangeException:找不到表 0
【发布时间】:2013-10-10 01:40:49
【问题描述】:

达尔码

 public DataSet selectlogin(string u_name, string u_password, string u_email, string action)
    {
        SqlConnection con = new SqlConnection(h);
        SqlCommand cmd = new SqlCommand("", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_login";
        cmd.Parameters.AddWithValue("@name", u_name);
        cmd.Parameters.AddWithValue("@email", u_email);
        cmd.Parameters.AddWithValue("@password", u_password);
        cmd.Parameters.AddWithValue("@action", action);
        con.Open();
        cmd.ExecuteNonQuery();

    DataSet ds = new DataSet();
    SqlDataAdapter ad = new SqlDataAdapter(cmd);
    ad.Fill(ds);
    return ds;
    con.Close();
}

巴尔代码

 public DataSet selectlogin(string u_name, string u_password, string u_email, string action)
    {
        DataSet ds = new DataSet();
        ds = obj.selectlogin(u_name, u_password, u_email, action);
        return ds;
    }

CS 代码

protected void Btn_log(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();

        ds = obj.selectlogin("", TextBox1.Text, TextBox2.Text,"login");

        if (ds.Tables[0].Rows.Count > 0)
        {
            Response.Redirect("dashboard.aspx");
        }


    }

存储过程

if(@action='login')

select * from login where u_email=@email and u_pass=@password

【问题讨论】:

  • 您可能想提供更多关于您的问题的文本。
  • 嗯.. 好代码!代码有问题吗?还是只是为了向我们展示您的代码?
  • 看起来你没有表0,也许你应该添加一个?您希望通过在此处发布此代码得到什么?
  • 如果没有用户提供的电子邮件和密码会怎样?数据集中没有返回表,因此您不能使用索引为零的表,因为没有表

标签: c# html asp.net ajax sql-server-2008


【解决方案1】:

问题可能在这里:

if (ds.Tables[0].Rows.Count > 0)

先检查索引为0的表是否存在,然后尝试访问属性...

if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 )

这应该会有所帮助。或者至少它会告诉你返回的数据集是空的(里面没有表)。

【讨论】:

    【解决方案2】:

    试试这个

    protected void Btn_log(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
    
            ds = obj.selectlogin("", TextBox1.Text, TextBox2.Text,"login");
        bool hasRows = ds.Tables.Cast<DataTable>()
                                   .Any(table => table.Rows.Count != 0);
            if (hasRows)
            {
                Response.Redirect("dashboard.aspx");
            }
    
        }
    

    或者试试看(用 != 运算符代替 > 运算符

    if (ds.Tables[0].Rows.Count **!=** 0)
            {
                Response.Redirect("dashboard.aspx");
            }
    

    【讨论】:

      【解决方案3】:

      CS 代码

      protected void Btn_log(object sender, EventArgs e)
      {
          DataSet ds = new DataSet();
      
          ds = obj.selectlogin("", TextBox1.Text, TextBox2.Text,"login");
      
          if (ds!=null && ds.Tables[0].Count > 0 && ds.Tables[0].Rows.Count > 0)
          {
              Response.Redirect("dashboard.aspx");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多