protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=MJ-PC;Initial Catalog=Test;Integrated Security=True ");
con.Open();
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
//SqlCommand cmd = con.CreateCommand();
SqlCommand cmd = new SqlCommand("select password from TestDemo where userName'" + txtusername .Text+ "'", con);
//cmd.Connection = con;
SqlDataReader da;
da = cmd.ExecuteReader();
if (!da.Read())
{
Response.Write("Wrong Details");
}
else
{
if(da[0].ToString()==txtusername.Text)
Response.Redirect("WebForm1.aspx");
else
Response.Write("Wrong Password");
}
}
-
Where's the error? At con.Open() or cmd.ExecuteReader()?unconnected– unconnected2013-11-02 06:25:38 +00:00Commented Nov 2, 2013 at 6:25
Add a comment
|
4 Answers
where username **=**
forgot equality sign
Also, the conenction you open and the connection you use are different
7 Comments
Aniket Inge
Heh, good catch. But still gotta figure out where he got an error. It could be a typo here.
LINQ2Vodka
does con.open() work fine? Also, assign resulting query to a string , debug, copy it tobuffer and run directly on sql via sql studio
Aniket Inge
Nah, I am very sure it won't work fine. See my answer for 'why'
LINQ2Vodka
Not sure i underatsnd you. Anyway, to build a proper connection string use VS integrated datasource wizard - just build it and test until it works. To make correct query try it on direct sql (like SSMS). If both work then do: 1) create and open connection 2) run query on it. Obviuosly :))
Aniket Inge
Well, if you see, he is trying to use
con object in btnsubmit_Click handler, but according to C# rules, you CANNOT access a local object defined in one method, and use it in another method |
The way I see it, you open a connection to the SQL server in Page_Load handler. But you don't close it.
If you try to open another one, or try to execute on a closed SqlConnection object, you might get an error.
A good way to do this is do something like this:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
//do something here
}
catch (Exception)
{
/*Handle error*/
}
}
Comments
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
try
{
con = new SqlConnection("Data Source=MJ-PC;Initial Catalog=Test;Integrated Security=True");
con.Open();
}
catch
{
//Handles exceptions here
}
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
try
{
//SqlCommand cmd = con.CreateCommand();
SqlCommand cmd = new SqlCommand("select password from TestDemo where userName='" + txtusername .Text+ "'", con);
//cmd.Connection = con;
SqlDataReader da;
da = cmd.ExecuteReader();
if (!da.Read())
{
Response.Write("Wrong Details");
}
else
{
if(da[0].ToString()==txtusername.Text)
Response.Redirect("WebForm1.aspx");
else
Response.Write("Wrong Password");
}
}
finally
{
con.Close();
}
}
3 Comments
Aniket Inge
How do you reference
con object in btnsubmit_Click()?Sudhakar Tillapudi
@Ankit : Please check it now.
Aniket Inge
If it were upto me, I would give it another -1, you do OPEN the connection, but you don't close it. When you will try to open it again ever in the future, you will get a yellow-red screen of death on asp.net
If you code for login, then here a neat version code, Depend on your flagset you can redirect or display wrong password msg
bool flagset=false;
SqlDataReader dr;
using (SqlConnection con = new SqlConnection(cn.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select password from TestDemo where userName=@uName";
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@uName", txtusername.Text);
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows){
while (dr.Read())
{
if(dr[0].ToString()==txtusername.Text)
{ flagset=true; }
}
}dr.Close();
con.Close();
}
}return flagset;