Results 1 to 6 of 6

Thread: Having a date as the primary key in a database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    65

    Having a date as the primary key in a database

    I have a table in my database and the primary key is the date. I have to access that row of the table to get data. But when ever i execute the reader and then get the value i get the error, No data exists for the row/column. I have tried all different ways of having the sql command look for that string but with no joy. Is what i'm doing possible?
    Last edited by RufusLDK; Mar 18th, 2007 at 02:09 PM.

  2. #2
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: Having a date as the primary key in a database

    If you post the code you are having trouble with we may be able to help.

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    65

    Re: Having a date as the primary key in a database

    Code:
    Public Sub addSale()
            conn.Open()
            sql = "SELECT sSold FROM Summary WHERE rDate =" & Date.Today
            Dim oc As New _
            System.Data.OleDb.OleDbCommand(sql, conn)
            Dim objRead As System.Data.OleDb.OleDbDataReader
            objRead = oc.ExecuteReader
            objRead.Read()
            intA = objRead.GetValue(0) + 1
            conn.Close()
            sql = "UPDATE Summary SET sSold = @sSold WHERE rDate =" & Date.Today
            Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
            cmd.Parameters.Add("@sSold", OleDb.OleDbType.Integer).Value = intA
            cmd.ExecuteNonQuery()
            conn.Close()
        End Sub
    I get the error at the underlined line

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Having a date as the primary key in a database

    NEVER use string concatenation to build SQL statements. It is insecure and error-prone, as you've just proven. Use parameters EVERY time.
    vb Code:
    1. Dim oc As New OleDbCommand("SELECT sSold FROM Summary WHERE rDate = @rDate", conn)
    2.  
    3. oc.Parameters.AddWithValue("@rDate", Date.Today)
    Also, if you're only getting a single value then you should be calling ExecuteScalar, not ExecuteReader.

    Also, there's no reason for you to be executing a query at all. If you want to update a single row then just execute an UPDATE statement. This should work I think:
    Code:
    UPDATE Summary SET sSold = sSold + 1 WHERE rDate = @rDate
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    65

    Re: Having a date as the primary key in a database

    Code:
        Public Sub addSale()
            conn.Open()
            Dim oc As New _
            System.Data.OleDb.OleDbCommand("SELECT sSold FROM Summary WHERE rDate =@rDate", conn)
            oc.Parameters.AddWithValue("@rDate", Date.Today)
            Dim objRead As System.Data.OleDb.OleDbDataReader
            objRead = oc.ExecuteReader
            objRead.Read()
            intA = objRead.GetValue(0) + (DataGridView1.Rows.Count - 1)
    
            Dim cmd As New _
            System.Data.OleDb.OleDbCommand("UPDATE Summary SET sSold = @sSold WHERE rDate = @rDate", conn)
            cmd.Parameters.AddWithValue("@rDate", Date.Today)
            cmd.Parameters.Add("@sSold", OleDb.OleDbType.Integer).Value = intA 
            cmd.ExecuteNonQuery()
            conn.Close()
    
        End Sub
    I'm having trouble updating the table now, i don't get any errors but it doesn't update the table. I've tested the first section of getting the data from the table so i can take it add to it then update it. But it doesn't enter the value to the table. Can anyone see where i'm going wrong

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Having a date as the primary key in a database

    What value does ExecuteNonQuery return?

    Also, you should be using ExecuteScalar like I said, not ExecuteReader.

    Finally, see this:
    vb Code:
    1. cmd.Parameters.AddWithValue("@rDate", Date.Today)
    then see this:
    vb Code:
    1. cmd.Parameters.Add("@sSold", OleDb.OleDbType.Integer).Value = intA
    then see this:
    vb Code:
    1. cmd.Parameters.AddWithValue("@sSold", intA)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width