Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Newby question

Reply
Thread Tools

Newby question

 
 
Hermann W Ehlers
Guest
Posts: n/a
 
      05-02-2004
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


 
Reply With Quote
 
 
 
 
DalePres
Guest
Posts: n/a
 
      05-02-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Matt Berther
Guest
Posts: n/a
 
      05-02-2004
Hello Hermann,

An dataset is probably complete overkill for what you're trying to accomplish 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 cast it to the appropriate type.

HTH,

--
Matt Berther
http://www.mattberther.com
 
Reply With Quote
 
Matt Berther
Guest
Posts: n/a
 
      05-02-2004
Hello DalePres,

Dale, in his code example, he was creating a SqlDataAdapter. The variable name 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 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

--

--
Matt Berther
http://www.mattberther.com
 
Reply With Quote
 
DalePres
Guest
Posts: n/a
 
      05-02-2004
You're right. I didn't catch that one at all. I guess that's why variable
naming is so important.

Dale

"Matt Berther" <(E-Mail Removed)> wrote in message
news:OQF5$(E-Mail Removed)...
> Hello DalePres,
>
> Dale, in his code example, he was creating a SqlDataAdapter. The variable

name 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >
> >> 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

> --
>
> --
> Matt Berther
> http://www.mattberther.com



 
Reply With Quote
 
Rick Spiewak
Guest
Posts: n/a
 
      05-02-2004
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/p...iences.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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Hermann Ehlers
Guest
Posts: n/a
 
      05-03-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Matt Berther
Guest
Posts: n/a
 
      05-03-2004
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();
}

> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 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

--

--
Matt Berther
http://www.mattberther.com
 
Reply With Quote
 
Hermann W Ehlers
Guest
Posts: n/a
 
      05-04-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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();
> }
>
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >
> >> 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

> --
>
> --
> Matt Berther
> http://www.mattberther.com



 
Reply With Quote
 
Matt Berther
Guest
Posts: n/a
 
      05-04-2004
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 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();
>> }
>>> 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" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> 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

>> --
>>
>> --
>> Matt Berther
>> http://www.mattberther.com

--

--
Matt Berther
http://www.mattberther.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a newby question Bruno Alexandre ASP .Net 2 05-02-2005 06:29 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Rephrase of Newby Question! John Baker ASP .Net 10 02-09-2005 12:55 AM
Newby Question Linking of data between HTML and ASP! John Baker ASP .Net 3 02-08-2005 01:13 PM
ASP.NET easy newby question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 6 11-23-2004 06:15 PM



Advertisments