2013-10-03 36 views
-7

德爾代碼System.IndexOutOfRangeException:無法找到表0

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 [email protected] and [email protected] 
+0

你可能想給更多的文本關於你的問題。 –

+0

嗯..不錯的代碼!代碼有問題嗎?還是僅僅向我們展示你的代碼? –

+0

看起來你沒有桌子0,也許你應該添加一個?你期望在這裏發佈這個代碼什麼? – iabbott

回答

1

麻煩可能會在這裏:

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

首先檢查是否爲0的索引表中存在,然後嘗試訪問的屬性...

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

這應該幫助。或者至少它會告訴你返回的數據集是空的(裏面沒有表)。

0

試試這個

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"); 
     } 
0

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"); 
    } 
}