View Full Version : Simple ASP.Net Access Database Question
jgallen23
09-14-2004, 09:52 AM
I'm fairly new to asp.net and I feel quite stupid for asking this because it was so easy in classic asp, rs("blah"), but I want to set the results from an sql statement to a variable. I've got my connection string, I just don't know where to go from there without using a datagrid
DBConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("data.mdb") & ";")
DBConnection.Open()
SQLString = "SELECT step_name FROM steps WHERE step_id = '1'"
and I want to set that result to a variable, step_name. Can anybody give me some assistance?
thanks,
jga
allida77
09-14-2004, 03:21 PM
If you are wanting to return just a single value use the ExecuteScalar (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbCommandClassExecuteScalarTopic.asp?frame=true) method.
Dim SQLString as String
Dim stepName as String
Dim DBConnection as OledbConnection= New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("data.mdb") & ";")
Dim commandObj as OledbCommand = New OledbCommand(SQLString,DBConnection)
Try
commandObj.Connection.Open
stepName = Convert.ToString(commandObj.ExecuteScalar)
Catch
Finally
DBConnection.Close
End Try
Check out the last paragraph in this article (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconadonetarchitecture.asp) . It will provide some other data objects to use in ado.net
jgallen23
09-14-2004, 04:51 PM
could you give me a general idea of what try, catch and finally do?
allida77
09-14-2004, 06:21 PM
A try catch block helps you in error handling in that you have the ability to catch any exceptions that may occur on a block of code. You can also catch multiple exceptions. The "Finally" portion will run every time even if an exception was thrown.
You can google "Try Catch vb.net" on google and come up with some better explanations/tutorials.
jgallen23
09-14-2004, 07:23 PM
another quick question...
I made the a function for getting the step name, and called it from page_load, and I'm getting the query string for step_id in the page_load as well, but it says that step_id in the GetStepName function is undefined. Is there something special I should do to make step_id a global variable for all functions to use?
allida77
09-14-2004, 07:50 PM
If you are going to be using it througout the page then declare it on the page:
Private _stepId as Integer
or if it is only used in this method pass it into function's signature:
Sub Page_Load(sender as object, e as eventargs)
GetStepName(Request.QueryString("step_id"))
End Sub
Protected Function GetStepName(stepid as Integer) as String
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.