Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 29th, 2005, 11:58 PM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rperreta
Default ASP .Net using VB .net to Connect To SQL Server

I am running SQL 2000 sp3a and VS .net 2003 I simply want to connect to my sql server and retrieve the data from a recordset. here is my code: Someone please help... :)

Imports System
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Data.SqlClient

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myDataReader As System.Data.SqlClient.SqlDataReader
        Dim myConnection As System.Data.SqlClient.SqlConnection
        Dim myCommand As System.Data.SqlClient.SqlCommand
        Dim strSQL As String

        myConnection = New SqlConnection("server=216.254.122.15; uid=sa; pwd=*****; database=filmgigs_db;")
        myCommand = New SqlCommand(strSQL, myConnection)

        strSQL = "SELECT * FROM genre where genreid=1"

        myConnection.Open()
        myDataReader = myCommand.ExecuteReader()
        TextBox1.Text = myDataReader.Item("genretype")

End Sub

HERE IS THE ERROR I GET

Invalid attempt to read when no data is present.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Invalid attempt to read when no data is present.

Source Error:

Line 49: myConnection.Open()
Line 50: myDataReader = myCommand.ExecuteReader()
Line 51: TextBox1.Text = myDataReader.Item("genretype")
Line 52:
Line 53: End Sub
 
Old March 30th, 2005, 07:05 AM
Authorized User
 
Join Date: Mar 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hy rperreta,

Try doing this :

1) I don't think that u need to declare reader,connection,
   command etc.. objects like this
   "Dim myDataReader As System.Data.SqlClient.SqlDataReader"
    since u've already imported the necessary files.
   they can b replaced by..
   "dim myDataReader as SqlDataReader" n wen u use them u can
    assign them memory by giving "new" like u've done in:
    "myCommand = New SqlCommand(strSQL, myConnection)"

2) reorder ur statements:

  - myConnection = New SqlConnection("server=216.254.122.15; uid=sa; pwd=*****; database=filmgigs_db;")
  - myConnection.Open()
  - strSQL = "SELECT * FROM genre where genreid=1"
  - myCommand = New SqlCommand(strSQL, myConnection)
  - myDataReader = myCommand.ExecuteReader()
  - TextBox1.Text = myDataReader.Item("genretype")

 Hope this helps.. :)

Ciao..



**************************************************
It bcums FATAL wen my brain puts my eyes on Call-Waiting ! >|B-)
 
Old March 30th, 2005, 10:33 PM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rperreta
Default

hmm... No dice... Still not working.. I get there is no data to read error. My SQL table is correct my statement is correct and there is data in the table. can someone post a simple way to read data from a table in sql 2000 sp3a.

thanks
rich

 
Old April 14th, 2005, 02:26 PM
Registered User
 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
If myDataReader.read() Then
  TextBox1.Text = myDataReader.Item("genretype")
Else
  'send message to user that no data was found
End If





Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to connect to asp.net development server Komila ASP.NET 3.5 Basics 5 April 15th, 2013 11:48 PM
unable to connect to asp.net development server Komila ASP.NET 3.5 Basics 4 April 15th, 2011 08:57 AM
ASP.NET 1.1,VB.NET,crystal reports, SQl server gvi Crystal Reports 1 September 11th, 2008 02:55 AM
How vb.net connect to the MySQL server database? cwliong2508 VB.NET 0 May 29th, 2006 07:58 PM
How to connect SQL Express in VB .NET/win Appli. venkikrao General .NET 0 January 4th, 2006 12:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.