Friday, March 30, 2012

having trouble connecting to database

Hi I am new at this.

I have a little program written in C# in asp.net. The program basically accesses a database and stores new records. The database is supposedly already attached to MSDE so I am able to see the tables of the database inside asp.net. I can click on the individual slots of the table and modify the datas manually. However, I want to connect to the database from my C# program and be able to input data into the database via the website that the C# program produces. After I type in the data into the website and click the submit button on the website, I get an error page that says this:

Login failed for user 'sa'

the line of code thats causing this error is:

con =

new SqlConnection("data source=(local)\\NetSdk; initial catalog=Friends; user id=sa");

Why is it not able to connect to the database?

YourChild:


con =

new SqlConnection("data source=(local)\\NetSdk; initial catalog=Friends; user id=sa");



I am taking the liberty to presume that your database name is Friends.I just couldn't figure what the "\\NetSdk" means. Perhaps you can addsomething to my knowledge.

Most probably if you aren't using Integrated Security for accessing thedatabase, you'd need to specify the password in the connection stringas well. You can make it something like this:

con =new SqlConnection("data source=(local);initial catalog=Friends;user id=sa;pwd="yourpassword")
Remember,it is a bad habit to store your connection string in the code. Tryputting it in the web.config, but you might want to do this later, whenyou get the hang of things.

Smiles.|||

uXuf:

YourChild:


con =

new SqlConnection("data source=(local)\\NetSdk; initial catalog=Friends; user id=sa");



I am taking the liberty to presume that your database name is Friends. I just couldn't figure what the "\\NetSdk" means. Perhaps you can add something to my knowledge.

Most probably if you aren't using Integrated Security for accessing the database, you'd need to specify the password in the connection string as well. You can make it something like this:

con =new SqlConnection("data source=(local);initial catalog=Friends;user id=sa;pwd="yourpassword")

Remember, it is a bad habit to store your connection string in the code. Try putting it in the web.config, but you might want to do this later, when you get the hang of things.

Smiles.

Thank you it works! I'm so glad you came by! now I am in the process of trying to stick the 'userName' and 'password' into the web.config file by using:

<

identityimpersonate="true"userName="sa"password="mypassword"/>

only now when I hit compile, Windows is giving me an error that says:

"Unable to start debugging on the web server. Server side-error occurred on sending debug HTTP request." Do I need to change something else to make this work?

There is another piece of code at the top of the web.config file that looks like this:

<authenticationmode="None"/>

Should "None" be set to "Windows"?

Then there is another username and password inside the "Directory Security" in IIS via 'property' for the virtual directory corresponding to the directory of the project. Is this username and password associated with the userName and password used to access the database?

No comments:

Post a Comment