开发者

Login Form error

I am creating a login form. Where if the Login-id and password matches with the values stored in Register table it retrieves all values from Register table and the result stores in the SqlDataReader object .So, now I want to access accountid and type from this table.How can we do it?

To understand what I am trying to say please have a look on the following code-

--------------------Login.aspx.cs---------------------------------------------------------------

protected void Submit_Click(object sender, EventArgs e)
{
string conStr = ConfigurationManager.ConnectionStrings["ROCConStr"].ConnectionString;
S开发者_StackOverflow中文版qlConnection scon = new SqlConnection(conStr);
scon.Open();
SqlCommand cmd = new SqlCommand("select* from Register where email= @login_id and pwd=@password", scon);
SqlParameter Para;
Para = cmd.Parameters.Add("@login_id", SqlDbType.Char);
Para.Value = email.Text;
Para = cmd.Parameters.Add("@password", SqlDbType.Char);
Para.Value = pwd.Text;
SqlDataReader reader = cmd.ExecuteReader();
if(reader.HasRows)
{if (reader["account_type"]=="admin") /* error - Please see the bottom for more details.*/
Response.Redirect("http://localhost:1187/roc/WelcomeAdmin.aspx");
else
Response.Redirect("http://localhost:1187/roc/WelcomeUser.aspx");}
else
{ Label1.Text = "UserID or password is incorrect."; }
reader.Close();
scon.Close();
}

==================================================

<br>About the erro:- reader["account_type"]=="admin"

I dont know how to retrieve the value of a single column.So, I just typed to show what I want to do. I tested this code I got the error - "Invalid attempt to read when no data is present."


You need to call reader.Read() to move to the first row.

Alternatively, change your query to select only the column you want, then call ExecuteScalar to return that value directly (or null if there were no rows) without using a DataReader at all.
(you can't do this, since you want more than one column)


You've only checked to see if you 'HasRows'; but you still need to advance the Reader. reader.Read()

That should fix your problem. You might want to also consider incorporating 'using' statements to limit the scope of your objects and modifying your URLs to use relative paths instead of absolute. That way, if you deploy to a new environment or move servers you don't have to remove the hard-coded URL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜