toraj58
November 27th, 2008, 08:29 PM
i have written this code to check the user that wants to log in but i think it is not efficient:
protected void Button1_Click(object sender, EventArgs e)
{
if (Button1.Text == "Sign out")
{
Session.Abandon();
Button1.Text = "Sign in";
//Label1.Text = "You signed out successfully!";
Label1.Visible = true;
Response.Redirect("~/default.aspx");
}
else
{
//Session["logged"] = true.ToString();
string constring = WebConfigurationManager.ConnectionStrings["gisConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
string selectCommand = "select id from members where username=@username and password=@password";
SqlCommand cmd = new SqlCommand(selectCommand, con);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
try
{
con.Open();
SqlDataReader data = cmd.ExecuteReader();
if (data.HasRows)
{
Session["logged"] = true.ToString();
Label1.Text = "Signed in!";
Label1.Visible = true;
Button1.Text = "Sign out";
// MessageBox.Show("hi!");
}
else
{
Session["logged"] = false.ToString();
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
finally
{
con.Close();
}
}
}
i have highlighted the line i think it is not efficient in RED.
i also test this approach:
i replaced the red line with this code:
int result = cmd.ExecuteNoneQuery();
and then i check result to see if it is greater than 0 to let the user log in but it is always 0 (or less than it) when i use ExecuteNoneQuery().
i don't want to load data to SqlDataReader because it takes memory, i just want to know whether the query has result or not. what i have done wrong with ExecuteNoneQuery() that always retruned value from it, is not greater than 0?
i guess that ExecuteNoneQuery() does not work with select statement; am i right?
any idea?
protected void Button1_Click(object sender, EventArgs e)
{
if (Button1.Text == "Sign out")
{
Session.Abandon();
Button1.Text = "Sign in";
//Label1.Text = "You signed out successfully!";
Label1.Visible = true;
Response.Redirect("~/default.aspx");
}
else
{
//Session["logged"] = true.ToString();
string constring = WebConfigurationManager.ConnectionStrings["gisConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
string selectCommand = "select id from members where username=@username and password=@password";
SqlCommand cmd = new SqlCommand(selectCommand, con);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
try
{
con.Open();
SqlDataReader data = cmd.ExecuteReader();
if (data.HasRows)
{
Session["logged"] = true.ToString();
Label1.Text = "Signed in!";
Label1.Visible = true;
Button1.Text = "Sign out";
// MessageBox.Show("hi!");
}
else
{
Session["logged"] = false.ToString();
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
finally
{
con.Close();
}
}
}
i have highlighted the line i think it is not efficient in RED.
i also test this approach:
i replaced the red line with this code:
int result = cmd.ExecuteNoneQuery();
and then i check result to see if it is greater than 0 to let the user log in but it is always 0 (or less than it) when i use ExecuteNoneQuery().
i don't want to load data to SqlDataReader because it takes memory, i just want to know whether the query has result or not. what i have done wrong with ExecuteNoneQuery() that always retruned value from it, is not greater than 0?
i guess that ExecuteNoneQuery() does not work with select statement; am i right?
any idea?