Thursday, March 22, 2012

Newby question

Hi
I need help at the very beginning. I have been writing classic ASP for some
time, but getting data out of a SQL database and displaying a single value
eludes me.
So far I have the following code:
Dim myConnection As New SqlConnection("UID=dbuser;PWD=thepass;Data
Source=SQLServer;Initial Catalog=database;")
Dim myCommand As New SqlDataAdapter("select Name from Products where code =
0117", myConnection)
Dim ds As New DataSet()
myCommand.Fill(ds, "Name")
to get a record out of the database. All I want to do now put the Name in
that record into a variable and use it in the HTML text. I would appreciate
any help.
Thank you
HermannFirst off, the Fill method is a method of the SqlDataAdapter class so to use
Fill, you'd have to create a data adapter first, but you don't need a
dataset for what you're doing. Use the ExecuteScalar() method of the
SqlCommand class to return a single value. Search the framework
documentation for "Obtaining a Single Value from a Database".
In order to use that server side variable in your HTML, do it just as you
would in classic ASP:
<p>Product name for product code 0117 is: <%= myVariable %>.</p>
Dale
"Hermann W Ehlers" <hermann@.AThawkspoint.com> wrote in message
news:usvpno9LEHA.340@.TK2MSFTNGP11.phx.gbl...
> Hi
> I need help at the very beginning. I have been writing classic ASP for
some
> time, but getting data out of a SQL database and displaying a single value
> eludes me.
> So far I have the following code:
> Dim myConnection As New SqlConnection("UID=dbuser;PWD=thepass;Data
> Source=SQLServer;Initial Catalog=database;")
> Dim myCommand As New SqlDataAdapter("select Name from Products where code
=
> 0117", myConnection)
> Dim ds As New DataSet()
> myCommand.Fill(ds, "Name")
> to get a record out of the database. All I want to do now put the Name in
> that record into a variable and use it in the HTML text. I would
appreciate
> any help.
> Thank you
> Hermann
>
Hello Hermann,
An dataset is probably complete overkill for what you're trying to accomplis
h here.
Try this:
(c# code, but VB should be pretty similar)
SqlConnection myConnection = new SqlConnection("UID=dbuser;PWD=thepass;Data
Source=server;Initial Catalog=database;");
SqlCommand cmd = new SqlCommand("select name from products where code = 0117
", myConnection);
string s = (string)cmd.ExecuteScalar();
ExecuteScalar will receive one value from a database. It is up to you to cas
t it to the appropriate type.
HTH,
Matt Berther
http://www.mattberther.com
Hello DalePres,
Dale, in his code example, he was creating a SqlDataAdapter. The variable na
me was just wrong. ;)
> First off, the Fill method is a method of the SqlDataAdapter class so
> to use Fill, you'd have to create a data adapter first, but you don't
> need a dataset for what you're doing. Use the ExecuteScalar() method
> of the SqlCommand class to return a single value. Search the
> framework documentation for "Obtaining a Single Value from a
> Database".
> In order to use that server side variable in your HTML, do it just as
> you would in classic ASP:
> <p>Product name for product code 0117 is: <%= myVariable %>.</p>
> Dale
> "Hermann W Ehlers" <hermann@.AThawkspoint.com> wrote in message
> news:usvpno9LEHA.340@.TK2MSFTNGP11.phx.gbl...
>
> some
>
> =
>
> appreciate
>
--
Matt Berther
http://www.mattberther.com
You're right. I didn't catch that one at all. I guess that's why variable
naming is so important.
Dale
"Matt Berther" <mberther@.hotmail.com> wrote in message
news:OQF5$FAMEHA.140@.TK2MSFTNGP09.phx.gbl...
> Hello DalePres,
> Dale, in his code example, he was creating a SqlDataAdapter. The variable
name was just wrong. ;)
>
> --
> --
> Matt Berther
> http://www.mattberther.com
You should look at the Microsoft Data Access Application Block. This
simplifies the process a great deal. In any case, whether you use the
application block or not, if you just want one value, you can use the
ExecuteScalar method of the SQLCommand class to readily return it, or the
equivalent method of the SQLHelper class defined in the Data Access
Application Block.
Take a look at
http://www.microsoft.com/resources/...diences.asp#dev
Scroll down a ways, and you'll find a link to the Data Access Application
Block, as well as a wealth of other useful information.
"Hermann W Ehlers" <hermann@.AThawkspoint.com> wrote in message
news:usvpno9LEHA.340@.TK2MSFTNGP11.phx.gbl...
> Hi
> I need help at the very beginning. I have been writing classic ASP for
some
> time, but getting data out of a SQL database and displaying a single value
> eludes me.
> So far I have the following code:
> Dim myConnection As New SqlConnection("UID=dbuser;PWD=thepass;Data
> Source=SQLServer;Initial Catalog=database;")
> Dim myCommand As New SqlDataAdapter("select Name from Products where code
=
> 0117", myConnection)
> Dim ds As New DataSet()
> myCommand.Fill(ds, "Name")
> to get a record out of the database. All I want to do now put the Name in
> that record into a variable and use it in the HTML text. I would
appreciate
> any help.
> Thank you
> Hermann
>
Hi
Thanks for your help. The code example comes straight out of a tutorial on
asp.net!
Unfortunately, I do not at this time have the leisure to learn the ins and
outs of the .net methods. All I need is the equivalent code to:
Set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open "dbname","username","password"
Query = "Select * from links Where ID = '" & id & "'"
Set RS = Connect.Execute(Query) <-- I thought this is the dataset
code = RS("code")
title = RS("title")
url = RS("url")
email = RS("email")
description = RS("description")
retlink = RS("retlink")
I would be very thankful if someone could help me write a the code to do the
same thing under .net.
Thanks
Hermann
"Hermann W Ehlers" <hermann@.AThawkspoint.com> wrote in message
news:usvpno9LEHA.340@.TK2MSFTNGP11.phx.gbl...
> Hi
> I need help at the very beginning. I have been writing classic ASP for
some
> time, but getting data out of a SQL database and displaying a single value
> eludes me.
> So far I have the following code:
> Dim myConnection As New SqlConnection("UID=dbuser;PWD=thepass;Data
> Source=SQLServer;Initial Catalog=database;")
> Dim myCommand As New SqlDataAdapter("select Name from Products where code
=
> 0117", myConnection)
> Dim ds As New DataSet()
> myCommand.Fill(ds, "Name")
> to get a record out of the database. All I want to do now put the Name in
> that record into a variable and use it in the HTML text. I would
appreciate
> any help.
> Thank you
> Hermann
>
Hello Hermann,
SqlConnection myConnection = new SqlConnection("UID=dbuser;PWD=thepass;Data
Source=server;Initial Catalog=database;");
SqlCommand cmd = new SqlCommand("select name from products where code = 0117
", myConnection);
using (IDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
rdr.Read(); // if you want to loop through records, sorround this with a whi
le block
string code = rdr["code"].ToString();
string title = rdr["title"].ToString();
string url = rdr["url"].ToString();
string email = rdr["email"].ToString();
string description = rdr["description"].ToString();
string retlink = rdr["retlink"].ToString();
}
> Hi
> Thanks for your help. The code example comes straight out of a
> tutorial on asp.net!
> Unfortunately, I do not at this time have the leisure to learn the ins
> and outs of the .net methods. All I need is the equivalent code to:
> Set Connect = Server.CreateObject("ADODB.Connection")
> Connect.Open "dbname","username","password"
> Query = "Select * from links Where ID = '" & id & "'"
> Set RS = Connect.Execute(Query) <-- I thought this is the dataset
> code = RS("code")
> title = RS("title")
> url = RS("url")
> email = RS("email")
> description = RS("description")
> retlink = RS("retlink")
> I would be very thankful if someone could help me write a the code to
> do the same thing under .net.
> Thanks
> Hermann
> "Hermann W Ehlers" <hermann@.AThawkspoint.com> wrote in message
> news:usvpno9LEHA.340@.TK2MSFTNGP11.phx.gbl...
>
> some
>
> =
>
> appreciate
>
--
Matt Berther
http://www.mattberther.com
Hi
Much Thanks for the code. I placed on a test page with the correct
variables, but received this error message:
Compiler Error Message: BC30684: 'SqlConnection' is a type and cannot be
used as an expression
I have the following lines at the top of the page:
<%@. import Namespace="System.Data" %>
<%@. import Namespace="System.Data.SqlClient" %>
Do I need to add anything else?
Thanks again
Hermann
"Matt Berther" <mberther@.hotmail.com> wrote in message
news:ONNBoITMEHA.2532@.TK2MSFTNGP10.phx.gbl...
> Hello Hermann,
> SqlConnection myConnection = new
SqlConnection("UID=dbuser;PWD=thepass;Data Source=server;Initial
Catalog=database;");
> SqlCommand cmd = new SqlCommand("select name from products where code =
0117", myConnection);
> using (IDataReader rdr =
cmd.ExecuteReader(CommandBehavior.CloseConnection))
> {
> rdr.Read(); // if you want to loop through records, sorround this with
a while block
> string code = rdr["code"].ToString();
> string title = rdr["title"].ToString();
> string url = rdr["url"].ToString();
> string email = rdr["email"].ToString();
> string description = rdr["description"].ToString();
> string retlink = rdr["retlink"].ToString();
> }
>
> --
> --
> Matt Berther
> http://www.mattberther.com
Hello Hermann,
Try putting this in the .cs code-behind file in Page_Load.
> Hi
> Much Thanks for the code. I placed on a test page with the correct
> variables, but received this error message:
> Compiler Error Message: BC30684: 'SqlConnection' is a type and cannot
> be used as an expression
> I have the following lines at the top of the page:
> <%@. import Namespace="System.Data" %>
> <%@. import Namespace="System.Data.SqlClient" %>
> Do I need to add anything else?
> Thanks again
> Hermann
> "Matt Berther" <mberther@.hotmail.com> wrote in message
> news:ONNBoITMEHA.2532@.TK2MSFTNGP10.phx.gbl...
>
> SqlConnection("UID=dbuser;PWD=thepass;Data Source=server;Initial
> Catalog=database;");
>
> 0117", myConnection);
>
> cmd.ExecuteReader(CommandBehavior.CloseConnection))
>
> a while block
>
--
Matt Berther
http://www.mattberther.com

0 comments:

Post a Comment