Newby question

  • Thread starter Hermann W Ehlers
  • Start date
H

Hermann W Ehlers

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
 
D

DalePres

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
 
M

Matt Berther

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,
 
M

Matt Berther

Hello DalePres,

Dale, in his code example, he was creating a SqlDataAdapter. The variable name was just wrong. ;)
 
D

DalePres

You're right. I didn't catch that one at all. I guess that's why variable
naming is so important.

Dale
 
R

Rick Spiewak

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/practices/audiences.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.
 
H

Hermann Ehlers

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
 
M

Matt Berther

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();
}
 
H

Hermann W Ehlers

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 said:
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
 
M

Matt Berther

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
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
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
 
H

Hermann W Ehlers

Hi

Can you explain what you mean with "Page_Load"?
Thanks
Hermann

Matt Berther said:
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
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
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
 
J

John Scalco

there is a Page_Load event, which you can implement. I believe he wants you
to put the code he provided into that event handler.

private void Page_Load (object sender, System.EventArgs e)
{
// your code here
}

this is part of the 'code behind' technology in ASP.net.

hope that helps,
sincerely,
J



Hermann W Ehlers said:
Hi

Can you explain what you mean with "Page_Load"?
Thanks
Hermann

Matt Berther said:
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

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
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
 
M

Matt Berther

Hello John,

Thanks. That is exactly what I meant...
there is a Page_Load event, which you can implement. I believe he
wants you to put the code he provided into that event handler.

private void Page_Load (object sender, System.EventArgs e)
{
// your code here
}
this is part of the 'code behind' technology in ASP.net.

hope that helps,
sincerely,
J
Hi

Can you explain what you mean with "Page_Load"?
Thanks
Hermann
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
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top