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