Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing a database field into a variable 1

Status
Not open for further replies.

fischadler

Programmer
May 31, 2002
258
MT
Sounds simple. At least it was simple with classic ASP. I am now trying to switch to the supposedly better ASP.NET but I cannot make a simple database connection and retrieve the value of a field.

I have looked through several pages using Google and all of them tell me how to store the contents of a database table into a datagrid or a repeater. But all I want is to store the contents of a single field of a single record recordset into a string variable.

I found this sample code below but I keep getting an error:

Code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
  sub Page_Load(sender as Object, e as EventArgs)
    Dim connString as String
    connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("data.mdb")
  
    Dim objConnection as OleDbConnection
    objConnection = New OleDbConnection(connString)
    objConnection.Open()   'open the connection

    'Specify our SQL statement
    Dim strSQL as String = "SELECT NewsID, NewsDate, NewsTitle, NewsIntro, NewsDetails FROM News WHERE (((NewsID)=1000));"
    
    'Create the Command object
    Dim objCommand as OleDbCommand
    objCommand = New OleDbCommand(strSQL, objConnection)
		
   ' Set an OleDbDataReader to the command's results
    Dim objDataReader as OleDbDataReader
    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
		
		Dim str as string
		
    While objDataReader.Read()
			str = objDataReader("NewsTitle")
    End While
    
		Response.Write(str)
		
		objDataReader.Close()
	end sub
</script>

I keep getting this error:
System.Data.OleDb.OleDbException: IErrorInfo.GetDescription failed with E_FAIL(0x80004005).

at this line:

Code:
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

What am I doing wrong? Note that I am totally new to ASP.NET and I am switching to it from Classic ASP.

Is there any better way to just store the contents of the field in a variable?

-Fischadler
 
That error seems to suggest there is an error with the provider when it actually executes the statement (i.e not an ASP.NET issue). You could try putting brackets around the field names as Access is particularly fussy when it comes to syntax (not to mention it's ability to cope in web applications.




____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
OK. Removing the brackets solved the problem. Thanks!

-Fischadler
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top