Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using below code but i am using windows authentication. visual studio is showing below error.

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"




Please let me know how to use windows authentication for database connection.

What I have tried:

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=hp-pc;database=admin.mdf");
con.Open();
string q = "insert into (user_id,name,password,email_id,contact,user_type) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
SqlCommand com = new SqlCommand(q, con);
com.ExecuteNonQuery();
Response.Write("alert('saved')");
Response.Redirect("index.aspx");
Posted
Updated 27-Jan-17 7:54am
Comments
[no name] 27-Jan-17 8:07am    
"windows authentication for database connection", you add your username and password to your connection string. Why you think that will fix your error is anyone's guess. And, you might want to research SQL injection attacks. You have a pretty nice opening for one going on there.
Member 12950401 27-Jan-17 8:53am    
Thank you
Jochen Arndt 27-Jan-17 8:14am    
See https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx
for the SQL connection string syntax.
Richard Deeming 27-Jan-17 9:33am    
Also, you're storing passwords in plain-text. You should only ever store a salted hash of the user's password, using a unique salt per record.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

1 solution

If you want to use windows authentication then you need to change the identity on your application pool in IIS to run using a windows account that has access to sql. The identity on the Application Pool is the account your ASP.Net code will run as.

However, you probably and usually want to use a sql account instead and place your connection string in the web.config.

Also, do the other things mentioned in the comments so you have good safe code.
 
Share this answer
 

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