Accessing one single value in a database, how difficult can it be?

Last post 05-16-2008 2:47 PM by dtromp. 5 replies.

Sort Posts:

  • Indifferent [:|] Accessing one single value in a database, how difficult can it be?

    05-08-2008, 10:17 AM
    • Loading...
    • dtromp
    • Joined on 05-08-2008, 10:05 AM
    • Posts 16
    I am completely new to ASP.NET. I reviewed a lot of information and got quite exiting when I saw how simple it is to create a gridview, attach it to a SQL Datasource and display/update/delete data through the gridview in the database.Having worked with that, I got to the stage where I programmatically needed to select a single value from a record in my SQL 2005 database. I know exactly which table, record and column I needed and thought, how difficult can it be?Turns out that there are many ways to Rome and none of them are short and easy. It seems to require quite a bit of coding but I wont believe that retrieving a single value from my database is that difficult, I am probably doing things wrong.Can someone show me the most simple, short and efficient code that you would normally use to retrieve one single value programmatically out of a SQL Database?

    Thanks in advance!

  • Re: Accessing one single value in a database, how difficult can it be?

    05-08-2008, 10:25 AM
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 6,695
    • Moderator
      TrustedFriends-MVPs

    If you're using 3.5, then it can be really simple with Linq. Here, I'm retreiving the ProductName of the Product with a ProductID equal to 1. 

    NorthwindDataContext db = new NorthwindDataContext();
    var productName = db.Products.Single(p => p.ProductID == 1).ProductName;
    Easy enough?
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: Accessing one single value in a database, how difficult can it be?

    05-08-2008, 10:29 AM
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 988

    Here's a simple example in c# using SqlCommand.ExecuteScalar:

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("SELECT ID FROM Tests WHERE (Colour = 'Pink')", conn);
    TextBox1.Text = Convert.ToString(cmd.ExecuteScalar());      // Put the ID value from the query in the textbox
    cmd.Dispose();
    conn.Dispose();

     

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: Accessing one single value in a database, how difficult can it be?

    05-08-2008, 5:28 PM
    • Loading...
    • dtromp
    • Joined on 05-08-2008, 10:05 AM
    • Posts 16

    I am sorry, I realized that I did not provided enough information. Hereby some more detail:

    I am using VB, not C. Unfortunately I can't use ASP.NET 3.5 as my provider only supports version 2.0. I am using Visual Studio 2005 to put my website together.

    This is the code that I used so far but I have the feeling that I am doing something way to complicated. Afterall,k I am only trying to retrieve one single string value out of a database using a SELECT command:

    Imports System.Data.SqlClient

    Dim connections As ConnectionStringSettingsCollection = _

    ConfigurationManager.ConnectionStrings

    Dim ConnectionString

    For Each connection As ConnectionStringSettings In connections

    If connection.Name = "ServerListConnectionString" Then

    ConnectionString = connection.ConnectionString

    Exit For

    End If

    Next

    Dim queryString As String = _

    "SELECT [BesrApproved] FROM [manualInfo] WHERE ([name] = @name)"

    Using connection As New SqlConnection(ConnectionString)

    Dim command As New SqlCommand(queryString, connection)

    command.Parameters.AddWithValue("@Name", TextBoxServerName.Text)

    connection.Open()

    Dim reader As SqlDataReader = command.ExecuteReader()

    ' Call Read before accessing data.

    While reader.Read()

    LabelUserName.Text = (reader.GetBoolean(0))

    End While

    ' Call Close when done reading.

    reader.Close()

     

  • Re: Accessing one single value in a database, how difficult can it be?

    05-08-2008, 5:43 PM
    Answer
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 988

    Your code is fine if it's working ok, and it's not too complicated really. Using a SqlDataReader is a perfectly valid way to get at your data. My only suggestion would be looking at SqlCommand.ExecuteScalar as I posted earlier. This returns a single value from a query so seems to be what you're looking for. Instead of using a SqlDataReader, you'd just use:

    LabelUserName.Text = Convert.ToString(command.ExecuteScalar())

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: Accessing one single value in a database, how difficult can it be?

    05-16-2008, 2:47 PM
    • Loading...
    • dtromp
    • Joined on 05-08-2008, 10:05 AM
    • Posts 16

    I wasnt able to work on the website for a while but I tried that command today and it worked like a charm. Thanks!

Page 1 of 1 (6 items)