Results 1 to 10 of 10

Thread: [RESOLVED] Edit Database Entry

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    58

    Resolved [RESOLVED] Edit Database Entry

    So when I export a database entry I want to change a value from either Null or 0 to 1. I have found tons of example on how to add entries, but I am not sure how to edit one.

    I currently I am thinking about just checking each value to see if it matches then deleting the record and adding a new one with the 1 instead of null/0. I just think there has to be a better way.

    I am using a MSSQL database.

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564

    Re: Edit Database Entry

    Sorry, misread your post, I thought you wanted help with the UPDATE function.

    Guess I'm not sure what you mean by Export a Database Entry

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    58

    Re: Edit Database Entry

    Well I have something in my database like:
    Code:
    JobNumber    Hours   Employee  Exported
    1            3       Joe       0
    3            5       Bob       0
    I read that and and write it to a .csv file.
    Once I write the first record to a text file I want to change it to:

    Code:
    JobNumber    Hours   Employee  Exported
    1            3       Joe       1
    3            5       Bob       0

  4. #4
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564

    Re: Edit Database Entry

    And how do you export? Do you do a SQL command and then save the results as a .txt/.csv file?

    If so, you can do a CASE and change the 0 to a 1.

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Edit Database Entry

    You want to export and write the 0 to 1 on the CSV, correct?
    Yeah what Brians says,how you do it? But to run a little ahead you can have your sql results to a datareader and loop to each row , put the result to the csv and change 0 to 1.Again on the next row, read datareader next line put results to csv, change 0 to 1.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    58

    Re: Edit Database Entry

    Sorry, I am a little confused. I read the database like this:
    vb Code:
    1. Using connection1 As New SqlConnection(Form1.ConnectionString)
    2.                             Using command1 As New SqlCommand("SELECT JobNumber, NameFirst, Hours FROM Employees", _
    3.                                                           connection1)
    4.                                 connection1.Open()
    5.                                 Using reader1 As SqlDataReader = command1.ExecuteReader()
    6.                                     While reader1.Read()
    7.                                         If JobNumber =  reader1("JobNumber") Then
    8.                                             aNameFirst = reader1("NameFirst")
    9.                                             aHours= reader1("Hours")
    10.                                         End If
    11.                                     End While
    12.                                 End Using
    13.                             End Using
    14.                         End Using

    I then use those variables to write the data to the csv. That part works fine.

    There is another column that I didn't read called exported. I just want to put a 0 there.

  7. #7
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564

    Re: Edit Database Entry

    Where do you read the Exported column?

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    58

    Re: Edit Database Entry

    Quote Originally Posted by BrianS View Post
    Where do you read the Exported column?
    Sorry, I should have given you my actual code, not just something similar.

    vb Code:
    1. For i = 0 To (ListView1.CheckedItems.Count - 1)
    2.             description = Form1.RemoveWhiteSpace(ListView1.CheckedItems(i).Text) & " " & Form1.RemoveWhiteSpace(ListView1.CheckedItems(i).SubItems(1).Text)
    3.  
    4.             Using connection As New SqlConnection(Form1.ConnectionString)
    5.                 Using command As New SqlCommand("SELECT TimeSheetID, ProjectNumber, CostCode, HoursTotal, Date FROM TimesheetDetails", _
    6.                                               connection)
    7.                     connection.Open()
    8.                     Using reader As SqlDataReader = command.ExecuteReader()
    9.                         While reader.Read()
    10.                             If reader("TimeSheetID") = ListView1.CheckedItems(i).SubItems(5).Text Then
    11.                                 jobnumber = reader("ProjectNumber")
    12.                                 costcode = reader("costcode")
    13.                                 accountingdate = reader("Date")
    14.                                 units = reader("HoursTotal")
    15.  
    16.                                 Using connection1 As New SqlConnection(Form1.ConnectionString)
    17.                                     Using command1 As New SqlCommand("SELECT EmployeeID, Rate FROM EmployeeRate", _
    18.                                                                   connection1)
    19.                                         connection1.Open()
    20.                                         Using reader1 As SqlDataReader = command1.ExecuteReader()
    21.                                             While reader1.Read()
    22.                                                 If ListView1.CheckedItems(i).SubItems(4).Text = reader1("EmployeeID") Then
    23.                                                     unitcost = reader1("Rate")
    24.                                                 End If
    25.                                             End While
    26.                                         End Using
    27.                                     End Using
    28.                                 End Using
    29.  
    30.                                 amount = units * unitcost
    31.                                 exportstring = "DC," & jobnumber & ",," & costcode & "," & "600" & "," & "2" & "," & transactiondate & "," & accountingdate & "," & description & "," & units & "," & unitcost & "," & amount & "," & "" & "," & ""
    32.                                 oWrite.WriteLine(exportstring)
    33.  
    34.                                 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    35.                                 '  The vlaues have been written, time to change the value of exported to a 1
    36.                                 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    37.                                 Using connection1 As New SqlConnection(Form1.ConnectionString)
    38.                                     Using command1 As New SqlCommand("SELECT EmployeeID, TimesheetID, Exported FROM Timesheet", _
    39.                                                                   connection1)
    40.                                         connection1.Open()
    41.                                         Using reader1 As SqlDataReader = command1.ExecuteReader()
    42.                                             While reader1.Read()
    43.                                                 If ListView1.CheckedItems(i).SubItems(4).Text = reader1("EmployeeID") Then
    44.                                                     If ListView1.CheckedItems(i).SubItems(5).Text = reader1("TimesheetID") Then
    45.                                                         Dim exported = reader1("Exported")
    46.                                                         ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    47.                                                         '  I can read the value with reader1("Exported") but I don't think that helps me
    48.                                                         ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    49.                                                     End If
    50.                                                 End If
    51.                                             End While
    52.                                         End Using
    53.                                     End Using
    54.                                 End Using
    55.  
    56.                             End If
    57.                         End While
    58.                     End Using
    59.                 End Using
    60.             End Using
    61.  
    62.         Next
    Last edited by dethredic; Apr 21st, 2010 at 09:31 AM.

  9. #9
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564

    Re: Edit Database Entry

    Try changing

    Code:
    "SELECT TimeSheetID, ProjectNumber, CostCode, HoursTotal, Date, Exported  FROM TimesheetDetails"
    to

    Code:
    "SELECT TimeSheetID, ProjectNumber, CostCode, HoursTotal, Date, case Exported when 0 then 1 when null then 1 else Exported end  FROM TimesheetDetails"

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    58

    Re: Edit Database Entry

    My ninja edit was a little slow. Anyways I figured it out:

    vb Code:
    1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    2.                                 '  The vlaues have been written, time to change the value of exported to a 1
    3.                                 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    4.                                 Using connection1 As New SqlConnection(Form1.ConnectionString)
    5.                                     Using command1 As New SqlCommand("SELECT EmployeeID, TimesheetID, Exported FROM Timesheet", _
    6.                                                                   connection1)
    7.  
    8.                                         connection1.Open()
    9.                                         Using reader1 As SqlDataReader = command1.ExecuteReader()
    10.                                             While reader1.Read()
    11.                                                 If ListView1.CheckedItems(i).SubItems(4).Text = reader1("EmployeeID") Then
    12.                                                     If ListView1.CheckedItems(i).SubItems(5).Text = reader1("TimesheetID") Then
    13.  
    14.                                                         Using connection2 As New SqlConnection(Form1.ConnectionString)
    15.  
    16.                                                             Using command2 As SqlCommand = New SqlCommand("UPDATE Timesheet SET Exported = 1 where EmployeeID = " & reader1("EmployeeID") & "and TimesheetID = " & reader1("TimesheetID"), connection2)
    17.                                                                 connection2.Open()
    18.                                                                 command2.ExecuteNonQuery()
    19.                                                                 connection2.Close()
    20.                                                             End Using
    21.                                                         End Using
    22.  
    23.                                                     End If
    24.                                                 End If
    25.                                             End While
    26.                                         End Using
    27.                                     End Using
    28.                                 End Using

    Thanks for the help.

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