4 Replies - 7042 Views - Last Post: 10 December 2008 - 12:55 PM Rate Topic: -----

#1 jdizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 08

ASP.NET ODBC Connection to MYSQL Works half the time

Posted 09 December 2008 - 02:50 PM

I am having a slight issue with a asp.net page i am writing. It used to work but now suddenly it doesnt. I am using inline code and obviously the page is uncompiled. So basically i have a mysql database that has data in it on a server and im using Visual Web Developer Express 2008 to write the code. I make the connection to the database, query it, store the records, and close the connection. My thougths where that this is the correct method. One of the following sql statements works flawlessly. The other it throws an "No Object Exists at this location" kind of error and well frankly i can literally take the sql statement open up mysql query browser, execute the query with no problems whatsoever. However if i add the little red circles on the left side and debug the code stepping through, piece by piece it sometimes works. I am so lost with this error and a week of searching has proven unsuccessful. I am using an MySqlODBC connecter 3.51 Here is my code, good luck.

<% 
					'Database connections strings
					Dim conStr As String

					conStr = "Driver={MySQL ODBC 3.51 Driver};SERVER=myserver.domain.com;PORT=3306;DATABASE=dhiintranetdb;UID=root;PWD=mypassword;OPTION=3;"

					Dim ODBCConn = New System.Data.Odbc.OdbcConnection(conStr)
					ODBCConn.Open()

					Try
						'Query database and store it in odbcdatacountreader()
						Dim OdbcCommandCount As New System.Data.Odbc.OdbcCommand("SELECT Id FROM RAInfo", ODBCConn)
						Dim OdbcDataCountReader = OdbcCommandCount.ExecuteReader()
						Dim index As Integer

					   'Loop to get number of records returned
						While OdbcDataCountReader.Read()
							index += 1
						End While
						OdbcDataCountReader.Close()

						Dim IdIndex As Integer

						'Generate random number from 0 to number of records returned (index)
						Dim random As New Random
						random = New Random()
						IdIndex = random.Next(0, index)
						
						'Create second sql string
						Dim sqlQuery As String = "SELECT * FROM RAInfo WHERE Id='" & IdIndex & "'"
   
						'Query database and store results in odbcdatareader()
						Dim OdbcCommand As New System.Data.Odbc.OdbcCommand(sqlQuery, ODBCConn)
						Dim OdbcDataReader = OdbcCommand.ExecuteReader()

						'Store results in strings
						Dim strTitle As String = OdbcDataReader.Item(1)
						Dim strDate As String = OdbcDataReader.Item(2)
						Dim str As String = OdbcDataReader.Item(3)
						
						'Output Html code
						Response.Write("<b>" & strTitle & "</b><br />")
						Response.Write("<br />")
						Response.Write("<u>" & strDate & "</u><br />")
						Response.Write("<br />")
						Response.Write(str)

						'Close connections
						OdbcDataReader.Close()
						ODBCConn.Close()
			
					Catch ex As Exception
						'Close database and redirect the website
						ODBCConn.Close()
						Response.Redirect("http://diaghyb-pwsrv:1080/moretocome.aspx")
			
					End Try
		 %>



Is This A Good Question/Topic? 0
  • +

Replies To: ASP.NET ODBC Connection to MYSQL Works half the time

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: ASP.NET ODBC Connection to MYSQL Works half the time

Posted 09 December 2008 - 06:27 PM

Which line in your code does the error point to?
Was This Post Helpful? 0
  • +
  • -

#3 jdizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 08

Re: ASP.NET ODBC Connection to MYSQL Works half the time

Posted 10 December 2008 - 09:57 AM

It errors out at the statement

Dim OdbcDataReader = OdbcCommand.ExecuteReader()

After that it throws the exception.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: ASP.NET ODBC Connection to MYSQL Works half the time

Posted 10 December 2008 - 11:31 AM

You have not declared the OdbcDataReader correctly.

Should be like this:
Dim OdbcDataCountReader As System.Data.Odbc.OdbcDataReader = OdbcCommandCount.ExecuteReader()

Was This Post Helpful? 0
  • +
  • -

#5 jdizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 08

Re: ASP.NET ODBC Connection to MYSQL Works half the time

Posted 10 December 2008 - 12:55 PM

Thanks for the quick response unfortunatly that doesnt seem to be the problem. I still get the {"No data exists for the row/column."} error in the exception. I have changed the code to the following.

'this line is actually not where the exception is
Dim OdbcDataCountReader As System.Data.Odbc.OdbcDataReader = OdbcCommandCount.ExecuteReader()

'this line is still where it throws the exception
Dim OdbcDataReader As System.Data.Odbc.OdbcDataReader = OdbcCommand.ExecuteReader()


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1