Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a new user to visual studio 2010 ASP.NET and I am struggling to connect to my database. The code I am testing to try and get it working is as follows:
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Award_Management2ConnectionString1"].ConnectionString);
        con.Open();
        string Usercommand = "select count(*) from Access_Control where User_Name ='" + TextBox1.Text + "'";
        SqlCommand Checkuser = new SqlCommand(Usercommand, con);
        int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());

        if (temp == 1)
        {
            string Passcommand = "select Access_Control.User_password from Access_Control where User_Name ='" + TextBox1.Text + "'";
            SqlCommand Checkpass = new SqlCommand(Passcommand, con);
            string password = Checkpass.ExecuteScalar().ToString();

            if (password == TextBox2.Text)....


Everytime I start without debugging, the login page for my webapp appears but as I log in I always get this error message :

Login failed for user 'MiThTiC-HP\AamirSQL'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'MiThTiC-HP\AamirSQL'.

Source Error: 


Line 24:     {
Line 25:         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Award_Management2ConnectionString1"].ConnectionString);
Line 26:         con.Open();
Line 27:         string Usercommand = "select count(*) from Access_Control where User_Name ='" + TextBox1.Text + "'";
Line 28:         SqlCommand Checkuser = new SqlCommand(Usercommand, con);



I am new to this so I am not sure what has gone wrong ... if someone could please help me that would be awesome !

Also I think I should NOTE: That the code which I have posted above works on my university server... I am trying to replicate that environment at home and I keep getting these errors
Posted
Updated 14-Jun-22 9:08am
v2
Comments
[no name] 29-Jul-13 7:23am    
"Login failed for user 'MiThTiC-HP\AamirSQL'", check your connection string.
Thanks7872 29-Jul-13 7:55am    
Remove all the code you posted above and post only the connection string you have used.
Nandakishore G N 29-Jul-13 8:28am    
check whether the login uid and pwd of the sql server is it correct?
venkateshCST 30-Jul-13 3:11am    
if you are getting conn string from webconfig then format is like
appsettings
add key="Award_Management2ConnectionString1" value="provider=System.Data.SqlClient;provider connection string="Data Source=Your server Name Catalog=DatabaseName;User ID=sa;Password=sasasa;
</appsettings></xml>"/
/appsettings
Aamir Mitha 12-Aug-13 4:18am    
Hey mate,
are you sure this is correct .. because when i put this in and re-build solution it says "unrecognized attribute 'string'. Note that attribute names are case-sensitive"

Hi Aamir,

Connection string is wrong in here I believe.
Try hard coding the connection string instead of
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Award_Management2ConnectionString1"].ConnectionString);

like this
SqlConnection con = new SqlConnection("data source=SQLExpress;User Id=sa;Password=123;Initial Catalog=DatabaseName;MultipleActiveResultSets=True");

If it is remote database then try connecting the database from your SQL Server IDE.

I hope this would help you a bit.
Thanks,
RK
 
Share this answer
 
Comments
Aamir Mitha 29-Jul-13 7:36am    
when I tried to hard code it I get this error :

The network path was not found

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ComponentModel.Win32Exception: The network path was not found

Just a noob question for Data Source should I be using SQLExpress or localhost ??? ... I tried both and with localhost I get the orginal error 'login fail for user' and with SQLexpress i get this new 'network path' error
Aamir Mitha 29-Jul-13 7:37am    
Also I think I should NOTE: That the code which I have posted above works on my university server... I am trying to replicate that environment at home and I keep getting these errors
Dholakiya Ankit 29-Jul-13 8:03am    
check solutions
♥…ЯҠ…♥ 29-Jul-13 8:21am    
Check whether sql server service is running in your machine? try to start the service, then go to sql server configuration manager --> sql server network configuration --> protocols for MSSqlServer --> enable all protocols and then try it. Same in SQL native client --> client protocols. Try it now and let me know
Aamir Mitha 12-Aug-13 4:02am    
Hey mate, all the protocols were already enabled.. I did not have to change a thing. This did not help.. as i got the same error page again
My solution what thanks to venkateshCST:

He told me to change my appsetting connectionstring within the web.config to be:
<add key="Award_Management2ConnectionString1" value="SERVER=(serverName);DATABASE=(DB name);UID=(userID);PASSWORD=(password)"/>


He also told me to change;
In places where I had -

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Award_Management2ConnectionString1"].ConnectionString);
        SqlConnection con = ConfigurationSettings.AppSettings["Award_Management2ConnectionString1"].ToString();
        sqlconnection con=new sqlconnection(constring);
        con.Open();


To -

string constring = ConfigurationManager.AppSettings["Award_Management2ConnectionString1"].ToString();
        SqlConnection con = new SqlConnection(constring);
        con.Open();
 
Share this answer
 
Try to specify your connection string as follows.


string connectionString = ConfigurationManager.ConnectionStrings["Award_Management2ConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);

Hope this will resolve your issue.
 
Share this answer
 
v2

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