Sunday, March 25, 2012

connecting to SQL db in an asp.net page

Ok, I'm an experienced asp developer switching to asp.net.

I've used the "Quickstart Tutorials" (http://www.asp.net/Tools/redir.aspx?path=quickstart) and have gotten started that way and built a few things based on that, but they only show XML connection info. Now I need to switch to SQL so I can connect to MS SQL Server for some stored procedures.

Anyone have suggestions?

(I've dug through some articles on msdn, etc, but I've only found so far theory and not code. And let me tell 'ya, I'm big on learning about code from code.)

If this helps, the specific tasks I'm trying to do are:
Have user select a static value from a drop down list, enter a word in a text box, and submit the form (so far so good), which connects to the db and returns the datagrid filled with results (the part I don't have). I already have my sp, just can't connect to it and get results back into the page.The Tutorials on this site have an example of doing just that. Check outPerforming a Parameterized Select

Sound like you have all of the pieces you need, you just need to stitch them together. Viewing the source code of that example should help you do just that. Post back if you are having trouble implementing it.

Terri|||I receive this error:

Server Error in '/Test1' Application.
------------------------

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested Url: /Test1/VB/datagrid2.aspx|||The problem is probably with the SqlConnection -- specifically, it is trying to read the connection string out of the web.config file. This is a good practice. But, let's try to get the sample running for you first.

Instead of this line:

MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PubsString"))

Try this line (assuming you are using a local SQL Server with Windows authentication)
MyConnection = New SqlClient.SqlConnection("server=(local);database=pubs;Trusted_Connection=yes")

Terri|||Ok, I'm looking at these various example ways to set up and connect to dbs.

I have a local sql server running on my machine.
I can use the sa account.
I have databases, sps, and tables already on it.

I've tried looking at the examples and changing to fit, but either I'm missing something or I've put info in the wrong place.

hum.|||YAY YAY YAY!!!

Thank you! I changed your line:
MyConnection = New SqlClient.SqlConnection("server=(local);database=pubs;Trusted_Connection=yes")

To:
MyConnection = New SqlClient.SqlConnection("server=(local);database=mydbname;uid=sa;pwd=sa;")

and it works in the datagrid2.aspx sample file! 8)

Whew!

Ok, now time to get this working in my files...

Thanks again!

No comments:

Post a Comment