Results 1 to 4 of 4

Thread: UPDATE SQLCommand

  1. #1

    Thread Starter
    Hyperactive Member SjR's Avatar
    Join Date
    Jul 2001
    Location
    Birmingham, UK
    Posts
    336

    UPDATE SQLCommand

    Hi there.
    I'm new to ADO.NET, and I'm trying to learn this without having to bother you lot on simple questions

    But..
    I'm having just a tiny problem with updating the SQL database. I'm trying to update 2 fields on a table "MatterDesc" and ModifiedDate". "MatterDesc" updates fine, "ModifiedDate" won't update. I think the problem is with the SQLParameter (highlighted in red in the code below).

    VB Code:
    1. Imports System.Data.SqlClient
    2.  
    3. Public Class frmPIMatter
    4.     Inherits System.Windows.Forms.Form
    5.  
    6.     Dim connCaseMan As SqlConnection = New SqlConnection("Data Source=BIRMDATA;Initial Catalog=CaseMan;Integrated Security=SSPI")
    7.     Dim cmdSelect As New SqlCommand()
    8.     Dim cmdUpdateMatter As New SqlCommand()
    9.     Dim cmdDelete As New SqlCommand()
    10.     Dim prmMatter As SqlParameter
    11.     Dim daMatter As New SqlDataAdapter()
    12.     Dim dsMatter As New DataSet()
    13.  
    14. #Region " Windows Form Designer generated code "
    15. ...
    16. #End Region
    17.  
    18.     Private Sub frmPIMatter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.  
    20.         Dim sSQL As String
    21.  
    22.         'Set up SQL parameters
    23.         'UPDATE Matter
    24.         sSQL = "UPDATE Matter SET "
    25.         sSQL = sSQL & "MatterDesc = @MatterDesc, "
    26.         sSQL = sSQL & "ModifiedDate = @ModifiedDate "
    27.         sSQL = sSQL & "WHERE MatterID = " & Me.Tag
    28.         cmdUpdateMatter = connCaseMan.CreateCommand
    29.         cmdUpdateMatter.CommandText() = sSQL
    30.         prmMatter = cmdUpdateMatter.Parameters.Add("@MatterDesc", SqlDbType.VarChar, 50, "MatterDesc")
    31.         [color=red]prmMatter = cmdUpdateMatter.Parameters.Add("@ModifiedDate", SqlDbType.DateTime, 8, "ModifiedDate")[/color]
    32.         prmMatter.SourceVersion = DataRowVersion.Original
    33.         daMatter.UpdateCommand = cmdUpdateMatter
    34.  
    35.         'SELECT
    36.         sSQL = "SELECT Matter.MatterDesc, "
    37.         sSQL = sSQL & "Matter.OpenDate, "
    38.         sSQL = sSQL & "Matter.ModifiedDate "
    39.         sSQL = sSQL & "FROM Matter "
    40.         sSQL = sSQL & "WHERE Matter.MatterID = " & Me.Tag
    41.  
    42.         cmdSelect = connCaseMan.CreateCommand
    43.         cmdSelect.CommandText = sSQL
    44.         daMatter.SelectCommand = cmdSelect
    45.         daMatter.Fill(dsMatter, "Matter")
    46.  
    47.         With Me
    48.             .Text = dsMatter.Tables(0).Rows(0).Item(0) & " [" & Format(.Tag, "000000") & "]"
    49.             .lblOpenDate.Text() = Format(dsMatter.Tables(0).Rows(0).Item(1), "ddd dd/MM/yyyy")
    50.             .txtMatterDesc.DataBindings.Add("Text", dsMatter.Tables(0), "MatterDesc")
    51.             .lblModifiedDate.DataBindings.Add("Text", dsMatter.Tables(0), "ModifiedDate") 'Displays OK when form has loaded - so is reading it OK from the database.
    52.         End With
    53.  
    54.     End Sub
    55.  
    56.     Private Sub frmPIMatter_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
    57.  
    58.         connCaseMan.Close()
    59.         cmdSelect.Dispose()
    60.         daMatter.Dispose()
    61.         dsMatter.Dispose()
    62.  
    63.     End Sub
    64.  
    65.     Private Sub tbPIMatter_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles tbPIMatter.ButtonClick
    66.  
    67.         Me.lblModifiedDate.Text = Format(Now, "dd/MM/yyyy HH:mm:ss")
    68.  
    69.         Me.BindingContext(dsMatter.Tables(0)).EndCurrentEdit()
    70.         daMatter.Update(dsMatter, "Matter")
    71.  
    72.         RefreshMatters(ctrlActiveScreen)
    73.  
    74.         Me.Close()
    75.  
    76.     End Sub
    77.  
    78. End Class

    Thanks for any help
    Another satisfied customer

  2. #2
    Member HumanCompiler's Avatar
    Join Date
    Dec 2002
    Location
    Decatur, IN USA
    Posts
    52
    Since you're using VS.NET, I would suggest you use its power. It can setup all of that for you.

    Simply go to the Data tab on your toolbar and drag out a SqlConnection, a SqlDatAdapter & two SqlCommands

    Give the SqlConnection it's ConnectionString (make sure it works). Do so by selecting it and changing it in the Properties window, then tell both of your SqlCommands to use the SqlConnection that you created as their connections, then click the elipses (...) on the CommandText property and build your sql statement or instead of using the Command Builder, just type in the Sql Statement you want. VS.NET will then go to your SqlServer and setup the parameters and all that jazz for you.

    Then tell the SqlDataAdapter which SqlCommands it should be using for it's SelectCommand and UpdateCommand.

    Then all your code will consist of is filling your DataSet and you're done...should work just fine and be less work for you
    -Erik Porter
    .NET MVP

  3. #3

    Thread Starter
    Hyperactive Member SjR's Avatar
    Join Date
    Jul 2001
    Location
    Birmingham, UK
    Posts
    336
    Cheers
    Thats made things a lot easier - and I thought creating the whole lot in code would be easier to understand whats going on..... The modified date updates OK now!
    Another satisfied customer

  4. #4
    Member HumanCompiler's Avatar
    Join Date
    Dec 2002
    Location
    Decatur, IN USA
    Posts
    52
    great, glad to hear it
    -Erik Porter
    .NET MVP

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