connecting SQLServer with Asp.net(C#)
-
hi all; I am trying to connect sql server2000 with asp.net(C#). but nothing happens. Actual problem comming from connection string which is not coming to solve my code is as follow: <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Page language="C#" %> void Page_Load() { string my="workstation id=HOME;packet size=4096;integrated security=SSPI;data source=HOME;persist security info=False;initial catalog=PrintQuota"; SqlConnection mycon=new SqlConnection(my); try { mycon.Open(); TextBox1.Text="Connection opend successfully"; mycon.Close(); } catch (Exception g) { TextBox1.Text=g.ToString(); } } The main problem is with user id and password,, i have also checked with integrated security=SSPI but no way to connect..the same string i use in window application but its work but here in web app its not working error message from exception is error System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\NETWORK SERVICE'. at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction) at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction) at System.Data.SqlClient.SqlConnection.Open() at ASP.UserInfo_aspx.Page_Load() in http://localhost/WebApplication1/UserInfo.aspx:line 16 plz solve my problem . i m waiting for replies. with regards Ishtiaq
-
hi all; I am trying to connect sql server2000 with asp.net(C#). but nothing happens. Actual problem comming from connection string which is not coming to solve my code is as follow: <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Page language="C#" %> void Page_Load() { string my="workstation id=HOME;packet size=4096;integrated security=SSPI;data source=HOME;persist security info=False;initial catalog=PrintQuota"; SqlConnection mycon=new SqlConnection(my); try { mycon.Open(); TextBox1.Text="Connection opend successfully"; mycon.Close(); } catch (Exception g) { TextBox1.Text=g.ToString(); } } The main problem is with user id and password,, i have also checked with integrated security=SSPI but no way to connect..the same string i use in window application but its work but here in web app its not working error message from exception is error System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\NETWORK SERVICE'. at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction) at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction) at System.Data.SqlClient.SqlConnection.Open() at ASP.UserInfo_aspx.Page_Load() in http://localhost/WebApplication1/UserInfo.aspx:line 16 plz solve my problem . i m waiting for replies. with regards Ishtiaq
Since you're using integrated security and ASP.NET is running under the NT AUTHORITY\NETWORK SERVICE account (must be IIS6 on Windows Server 2003), the SQL Server won't authenticate it you because 1) it doesn't have an account added for that user account, and 2) because NETWORK SERVICE is a local account - not a domain account. If SQL Server is on your web machine, you could get away with doing that, but it won't work if it's on another machine. Either use a specific username and password (typical on web machines) or impersonate a user. You can do this using the
<identity>
element in your application's (or web site's) Web.config file. You could also impersonate any requesting user and let SQL Server decide if they can query the data. It really depends on your requirements. To encrypt the connection string with the username and password in the registry and have ASP.NET use that instead of putting the plain-text string on the server, read http://support.microsoft.com/default.aspx?scid=kb;en-us;329290[^]. In the future, however, please post ASP.NET-related questions to the ASP.NET[^] forum. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]