Solved

Connection to MS SQL on page load

Posted on 2011-02-18
5
386 Views
Last Modified: 2012-05-11
Hi

I'm a newbie to asp.net moving from classic asp.

I've got my head around controls but need a bit more flexibility. I would really like to be able to connect to the database and query the database without using a control - can any point me in the right direction as I appear tom have met a large brick wall!

I'm using VWD 2010 Express & MS SQL

Many thanks in advance

Grant
0
Comment
Question by:Buzby58
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34925513
You can find a sample of talking to a database here (it's in C# but I can post a VB equivalent if needed):

     http://www.myvirtualplayground.co.uk/blog/fundamentals_reading_from_a_database_part_1
0
 

Author Comment

by:Buzby58
ID: 34925579
Hi Carl

Looks like just the sort of simple explanation I need - I'm working in VB so if it isn't too much hassle could I trouble you for that version!

Cheers

Grant
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34925647
Direct translation would be:
Dim connString As String = "Server=(local);Initial Catalog=AdventureWorks;Integrated Security=SSPI"
Dim conn As New SqlConnection(connString)

Dim cmd As New SqlCommand("SELECT [FirstName], [LastName] FROM [Person].[Contact]", conn)

conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()

While reader.Read()
      '// Method for reading fields same as for C# version
End While

conn.Close()

Open in new window

0
 

Author Comment

by:Buzby58
ID: 34926688
Easy when you know how . . . .

After much head scratching I've got it working in my aspx.vb file and loading with the required page.

Thank you for you help and the excellent primer article which I've also bookmarked - both much appreciated!

Cheers

Grant
0
 

Author Closing Comment

by:Buzby58
ID: 34926703
Really helpful and gave me a good steer to sorting out by problem! A big thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question