Newby question

Discussion in 'ASP .Net' started by Hermann W Ehlers, May 2, 2004.

  1. 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
    Hermann W Ehlers, May 2, 2004
    #1
    1. Advertising

  2. Hermann W Ehlers

    DalePres Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
    DalePres, May 2, 2004
    #2
    1. Advertising

  3. Hermann W Ehlers

    Matt Berther Guest

    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
    Matt Berther, May 2, 2004
    #3
  4. Hermann W Ehlers

    Matt Berther Guest

    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" <> wrote in message
    > news:...
    >
    >> 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, May 2, 2004
    #4
  5. Hermann W Ehlers

    DalePres Guest

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

    Dale

    "Matt Berther" <> wrote in message
    news:OQF5$...
    > 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" <> wrote in message
    > > news:...
    > >
    > >> 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
    DalePres, May 2, 2004
    #5
  6. Hermann W Ehlers

    Rick Spiewak Guest

    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.

    "Hermann W Ehlers" <> wrote in message
    news:...
    > 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
    >
    >
    Rick Spiewak, May 2, 2004
    #6
  7. 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" <> wrote in message
    news:...
    > 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
    >
    >
    Hermann Ehlers, May 3, 2004
    #7
  8. Hermann W Ehlers

    Matt Berther Guest

    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" <> wrote in message
    > news:...
    >
    >> 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, May 3, 2004
    #8
  9. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > > news:...
    > >
    > >> 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
    Hermann W Ehlers, May 4, 2004
    #9
  10. Hermann W Ehlers

    Matt Berther Guest

    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" <> wrote in message
    > news:...
    >
    >> 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" <> wrote in message
    >>> news:...
    >>>> 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
    Matt Berther, May 4, 2004
    #10
  11. Hi

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

    "Matt Berther" <> wrote in message
    news:%...
    > 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" <> wrote in message
    > > news:...
    > >
    > >> 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" <> wrote in message
    > >>> news:...
    > >>>> 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
    Hermann W Ehlers, May 5, 2004
    #11
  12. Hermann W Ehlers

    John Scalco Guest

    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" <> wrote in message
    news:...
    > Hi
    >
    > Can you explain what you mean with "Page_Load"?
    > Thanks
    > Hermann
    >
    > "Matt Berther" <> wrote in message
    > news:%...
    > > 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" <> wrote in message
    > > > news:...
    > > >
    > > >> 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" <> wrote in message
    > > >>> news:...
    > > >>>> 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

    >
    >
    John Scalco, May 5, 2004
    #12
  13. Hermann W Ehlers

    Matt Berther Guest

    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
    > "Hermann W Ehlers" <> wrote in message
    > news:...
    >
    >> Hi
    >>
    >> Can you explain what you mean with "Page_Load"?
    >> Thanks
    >> Hermann
    >> "Matt Berther" <> wrote in message
    >> news:%...
    >>
    >>> 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" <> wrote in message
    >>>> news:...
    >>>>> 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" <> wrote in message
    >>>>>> news:...
    >>>>>>> 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

    --

    --
    Matt Berther
    http://www.mattberther.com
    Matt Berther, May 5, 2004
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    ASP.NET easy newby question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Nov 22, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    474
    Kevin Spencer
    Nov 23, 2004
  2. John Baker
    Replies:
    3
    Views:
    304
    Kevin Spencer
    Feb 8, 2005
  3. John Baker

    Rephrase of Newby Question!

    John Baker, Feb 8, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    561
    Michael C#
    Feb 9, 2005
  4. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    646
    Iain Norman
    Feb 16, 2005
  5. Bruno Alexandre

    a newby question

    Bruno Alexandre, May 2, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    363
    Bruno Alexandre
    May 2, 2005
Loading...

Share This Page