2011-01-07 143 views
3

我試圖在ASP.NET網站上創建一個登錄表單。目前,有一些問題。我正在嘗試合併功能,以便登錄用戶有權查看他的個人資料。在登錄頁面上的代碼是這樣的:當dr中沒有數據時無效讀取

business.clsprofiles obj = new business.clsprofiles(); 
     Int32 a = obj.logincheck(TextBox3.Text, TextBox4.Text); 
     if (a == -1) 
     { 
      Label1.Text = "Username/Password incorrect"; 
     } 
     else 
     { 
      Session["cod"]= a; 
      Response.Redirect("profile.aspx"); 
     } 

登錄後,用戶被移動到頁面裏的人可以查看他的個人資料一旦登錄會話被正確地獲取登錄的人從價值登錄頁面併成功將其傳遞到下一頁。但這裏有個資料頁上發生了錯誤,我認爲有問題的地方在下面

public void grid_bind() 
{ 
    business.clsprofiles obj = new business.clsprofiles(); 
    List<business.clsprofilesprp> objprp = new List<business.clsprofilesprp>(); 
    Int32 z = Convert.ToInt32(Session["cod"]); 
    objprp = obj.fnd_profiles(z); //This line of code is passing an integer as required but does not get the desired result from the database 
    GridView1.DataSource = objprp; 
    GridView1.DataBind(); 
} 

按照商業邏輯錯誤稱,grid_bind()方法「無效嘗試當沒有數據在醫生的存在是爲了讀」

public List<clsprofilesprp> fnd_profiles(Int32 id) 
     { 
      if (con.State == ConnectionState.Closed) 
      { 
       con.Open(); 
      } 
      SqlCommand cmd = new SqlCommand("fndpro", con); 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.Add("@id", SqlDbType.Int).Value = id; 
      SqlDataReader dr = cmd.ExecuteReader(); 
      List<clsprofilesprp> obj = new List<clsprofilesprp>(); 
      while(dr.HasRows) 
      { 
       clsprofilesprp k = new clsprofilesprp(); 

       k.id = Convert.ToInt32(dr[0]);//Something wrong here? 

       k.name = dr[1].ToString(); 
       k.password = dr[2].ToString(); 
       k.description = dr[3].ToString(); 
       k.created = Convert.ToDateTime(dr[4]); 
       k.modified = Convert.ToDateTime(dr[5]); 
       obj.Add(k); 
      } 
      dr.Close(); 
      cmd.Dispose(); 
      con.Close(); 
      return obj; 
     }lesprp k = new clsprofilesprp(); 

     k.id = Convert.ToInt32(dr[0]);//Something wrong here? 

       k.name = dr[1].ToString(); 
       k.password = dr[2].ToString(); 
       k.description = dr[3].ToString(); 
       k.created = Convert.ToDateTime(dr[4]); 
       k.modified = Convert.ToDateTime(dr[5]); 
       obj.Add(k); 
      } 
      dr.Close(); 
      cmd.Dispose(); 
      con.Close(); 
      return obj; 
+0

可能存在重複[無數據存在時無效嘗試] (http://stackoverflow.com/questions/1147615/invalid-attempt-to-read-when-no-data-is-present) – 2014-01-24 08:10:03

回答

11

你必須調用DataReader.Read獲取結果:

SqlDataReader dr = cmd.ExecuteReader(); 
dr.Read(); 
// ... 

DataReader.Read返回一個布爾值,所以如果你有超過1分的結果,你可以這樣做:

While (dr.Read()) 
{ 
    // read data for each record here 
} 

而且,你」重新嘗試訪問dr數據時還有沒有在這部分代碼:

k.id = Convert.ToInt32(dr[0]);//Something wrong here? 
k.name = dr[1].ToString(); 
k.password = dr[2].ToString(); 
k.description = dr[3].ToString(); 
k.created = Convert.ToDateTime(dr[4]); 
k.modified = Convert.ToDateTime(dr[5]) 
0

您試圖訪問DataReader的一排,雖然沒有行。即 如果dr沒有進入while循環,那麼在datareader中沒有行,但是你仍然在訪問你有註釋的字段「//這裏有什麼錯誤?」。

3

你有一個問題...

while (dr.HasRows) 
{ 
    /* If this loop is entered, it will run 
    * indefinitely until the datareader miraculously 
    * loses all its rows in a hole somewhere */ 
} 

這要麼不會進入,或將創建一個無限循環......它或者有沒有行或有行。我想你的意思是:

while (dr.Read()) 
{ 
    /* Do something with the current record */ 
} 

dr.Read()循環到下一個記錄並返回true或取決於是否有要讀取或不是記錄假的。當數據讀取器初始化時,第一條記錄是而不是被選中。必須通過調用dr.Read()來選擇它,如果找到第一行,那麼它將返回true,並且確實將返回true,直到當前在最後一行時調用Read() - 即沒有更多行需要讀取。

dr.HasRows只是一個屬性,告訴你,如果datareader包含行......如果它確實會保持行從這裏直到永恆。例如,如果您想在某些情況下執行某些操作(如果它不包含行和其他內容),則可以使用此功能:

if (dr.HasRows) 
{ 
    while (dr.Read()) 
    { 
     /* Display data for current row */ 
    } 
} 
else 
{ 
    Console.WriteLine("I didn't find any relevant data."); 
} 
相關問題