Click here to Skip to main content
14,928,868 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
this is the code of login but it is not working can u please help me out...

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class sigin_in : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
   
    }
    SqlConnection con = new SqlConnection("Data Source=KRISHN-PC;Initial Catalog=sign;Persist Security Info=True;User ID=sa;Password=***********");
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataAdapter obj = new SqlDataAdapter("select from login where userid = '"+TextBox1.Text+"'and password = '"+TextBox2.Text +"'",con);
        DataSet a = new DataSet();
        obj.Fill(a);
        //obj.Update(a);
        if (a.Tables[0].Rows.Count > 0)
        {
            Response.Redirect("home.aspx");
        }
        else
        {
            Label.Text = " wrong pswd";
        }
    }

}
Posted
Updated 22-Jul-11 12:24pm
v2
Comments
Herman<T>.Instance 22-Jul-11 8:16am
   
select from login where userid --> leads to error because no fields are defined
krishn2011 22-Jul-11 12:56pm
   
dear i have given the field = to "textbox1.text"
Herman<T>.Instance 22-Jul-11 16:58pm
   
sql is something like select [fieldname] from login where.....

a SqlDataAdapter needs an Open() SqlConnection and after the data is in the dataset the SqlConnection needs to be Closed()

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
  
 public partial class sigin_in : System.Web.UI.Page
 {
	string connectToDB = "Data Source=KRISHN-PC;Initial Catalog=sign;Persist Security Info=True;User ID=sa;Password=***********";
	protected void Page_Load(object sender, EventArgs e)
	{

	}
 
	protected void Button1_Click(object sender, EventArgs e)
	{
		SqlConnection con = new SqlConnection(connectToDB);
		string myQuery = string.Format("select from login where userid = '{0}' and password = '{1}'", TextBox1.Text, TextBox2.Text);
		SqlDataAdapter obj = new SqlDataAdapter(myQuery, con);
		try
		{
			con.Open()
			DataSet a = new DataSet();
			obj.Fill(a);
			
			if (a.Tables[0].Rows.Count > 0)
			{
				Response.Redirect("home.aspx");
			}
			else
			{
				Label.Text = " wrong pswd";
			}
		}
		catch (Exception err)
		{
		}
		finally
		{
			if (null != con && con.Connection.State == ConnectionState.Open)
				con.Close();
		}
	 }
 } 
   
v2
Comments
krishn2011 22-Jul-11 12:44pm
   
dear this is nt right..
Herman<T>.Instance 22-Jul-11 16:57pm
   
explain please
Although this isn't a solution, I didn't want to add it as a plain comment, as I feel it is to important to go there.

Don't connect to any database using the sa account - also this account needs to be renamed to something else at least.

If you can't use integrated security then create a specific accounts for your database and make sure you grant the least amount of privaleges to each as possibly.

Have a google to learn more about securing your databases.
   
Comments
Herman<T>.Instance 22-Jul-11 9:32am
   
real good advice
krishn2011 22-Jul-11 12:58pm
   
yeah ..but we cant change the user name for your one program ...
Call the SqlConnection's Open method before passing it on to the SqlDataAdapter's constructor. SqlDataAdapter requires an open connection. And also specify the Column names in your select statement.
   
Your SQL 'Select' statement does not provide any field name.

'Select from login where ........' is a invalid sql statement.
   
Comments
krishn2011 22-Jul-11 12:43pm
   
dear it is ....and this is right... Select * from login where userid = '"+TextBox1.Text+"'and password = '"+TextBox2.Text +"'",con
web works 11-Aug-11 13:13pm
   
i don't see * in your select statement

string myQuery = string.Format("select from login where userid = '{0}' and password = '{1}'", TextBox1.Text, TextBox2.Text);

code
hey friends this code is right ...this code works..thanks


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class sigin_in : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
   
    }
    SqlConnection con = new SqlConnection("Data Source=KRISHN-PC;Initial Catalog=sign;Persist Security Info=True;User ID=sa;Password=12345");
    protected void Button1_Click(object sender, EventArgs e)
    {
        //con.Open();
        SqlDataAdapter obj = new SqlDataAdapter("Select * from login where userid = '"+TextBox1.Text+"'and password = '"+TextBox2.Text +"'",con);
        DataSet a = new DataSet();
        obj.Fill(a);
        //con.Close();
        //obj.Update(a);
        if (a.Tables[0].Rows.Count > 0)
        {
            Response.Redirect("home.aspx");
        }
        else
        {
            Label.Text = " wrong pswd";
        }
    }

}
   
v2
Try removing the ***** in password field of connection string and type actual password......hope it will work.
   
Comments
Herman<T>.Instance 22-Jul-11 8:14am
   
Something tells me he did not want to provide the password to us
You can use this code
SqlConnection con = new SqlConnection("Data Source=KRISHN-PC;Initial Catalog=sign;Persist Security Info=True;User ID=sa;Password=***********");
con.open();
string str=
SqlCommand cmd=new SqlCommand(select from login where userid = '"+TextBox1.Text+"'and password = '"+TextBox2.Text +"'",con);
SqlDataRaeder dr=cmd.ExecuteReader();
if(dr.Read())
{
Response.Redirect("home.aspx");
con.Close();
}
<else>
{
Label.Text = " wrong pswd"
con.Close();
}</else>
   
here u have to open the connection and give completed pwd instead of giving **

C++
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class sigin_in : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
SqlConnection con = new SqlConnection("Data Source=KRISHN-PC;Initial Catalog=sign;Persist Security Info=True;User ID=sa;Password=giveurpwd");
con.Open();----------->Open connection
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataAdapter obj = new SqlDataAdapter("select from login where userid = '"+TextBox1.Text+"'and password = '"+TextBox2.Text +"'",con);
DataSet a = new DataSet();
obj.Fill(a);
//obj.Update(a);
if (a.Tables[0].Rows.Count > 0)
{
Response.Redirect("home.aspx");
}
else
{
Label.Text = " wrong pswd";
}
}

} .
   
Comments
Herman<T>.Instance 22-Jul-11 8:26am
   
where is your connection closed? If you used a using() around the SqlConnection it was ok.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900