Friday, March 23, 2012

MSDE Problem

I am running MSDE (SQL Agent) and I am having the following problem:

When I setup my connection string to my table, it says that the connection was succesful. I have a asp.net page that has a text box and a button (trying to learn how to add data to a SQL table), anyways, when I input data into the text box and hit the button for it to add the data to the table I get the following error:

Error:
Login failed for user 'Discovery3\ASPNET'
Exception Details: System.Data.SqlCLient.SqlException: Login failed for user 'Discovery3\ASPNET'.

The line that is highlighted is
myConnection.Open()

Here is the code that I am using:
<>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myConnectionString As String

If myConnectionString = "" Then
myConnectionString = "server=Discovery3;database=dbDVD;trusted_Connection=true"
End If

Dim myConnection As New Data.SqlClient.SqlConnection(myConnectionString)
Dim myInsertQuery As String = "INSERT INTO tblMaster(TITLE) values('" & TextBox1.Text & "')"
Dim myCommand As New Data.SqlClient.SqlCommand(myInsertQuery)
myCommand.Connection = myConnection
myConnection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
End Sub

I don't understand how the connection can be succesfull, yet it errors out at the myConnection.Open() code. My table contains one field TITLE with nothing special.

Any ideas?The ASPNet user must be a user/login on the database - -|||There are a couple of solutions.

1. Go the the SQL Server and grant the ASPNET user Windows Integrated access to the server and the database "dbDVD". More general info on ASP.NET with MSDE can be found onKB 872905.
2. Use SQL authentication instead (less secure): add a SQL account to the database server, grant that account (read/write) access to the database. Change the connection string by omitting the trusted_connection flag and add a uid and pwd flag containing the user name (SQL account) and the password. To do this, you'll have to tweak the MSDE installation to allow SQL auth (you can MSN for this to find more info). However, I don't recommend this second solution.

Make sure only to grant the required access to the database that is needed from the web app; in this case likely only read/write to the tables (lowest privileges access).|||Thanks, this worked...

I got passed the permission error, but now I am getting one for the tables.

Here is the error I get now:

INSERT permission denied on object 'tblMaster', database 'master', owner 'dbo'

how do I give read/write access?

Thanks|||As explained in the aforementioned KB article, you'll need to grant access for the ASPNET login to the database (in this case "master" - side tip: I recommend to put your own private data in another database than the master!).

osql -E -S COMPUTER_NAME\VSDOTNET -Q "sp_grantlogin 'COMPUTER_NAME\ASPNET'"
osql -E -S COMPUTER_NAME\VSDOTNET -d Pubs -Q "sp_grantdbaccess 'COMPUTER_NAME\ASPNET'"
osql -E -S COMPUTER_NAME\VSDOTNET -d Pubs -Q "sp_addrolemember 'db_owner' 'COMPUTER_NAME\ASPNET'"

In here, replace the COMPUTER_NAME with the name of your computer and VSDOTNET with the name of the MSDE instance. In the second command, replace Pubs with the tables where you want to grant the ASPNET account access to.|||Thanks, I got the first to perfect it was the 'db_owner' that corrected the issue.

I appreciate the help.

No comments:

Post a Comment