Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Update Data in a Datagrid

Update Data in a Datagrid

Scheduled Pinned Locked Moved Visual Basic
databaseannouncement
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Larry White
    wrote on last edited by
    #1

    I am having difficulty getting the underlying SQL data to update when a change is made to the datagrid. The change is made using a combobox, and allows the proper selection of 1 of the 3 choices, but I can't seem to get the syntax/code right to update the table. Here is my current code: Public Sub comboControl_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboControl.SelectedValueChanged grdVW_AllOpen(hitTestGrid.Row, hitTestGrid.Column) = comboControl.Text Dim sc1 As New SqlCommand Dim sCnnStr As String sCnnStr = "Data Source=" & SQL_SERVER & ";Initial Catalog=" & DATABASE & ";User id=" & USER_ID & ";Password=" & PWD Dim sqlConn As New SqlConnection(sCnnStr) sc1.Connection = sqlConn sc1.CommandType = CommandType.Text sc1.CommandText = "UPDATE dbo.Chits_Backup SET Status = '" & comboControl.Text & "'" & ", LastName = @LastName, FirstName = @FirstName, DateofRequest = @DateofRequest, FromDate = @FromDate, ToDate = @ToDate, Days = @Days, NatureofRequest = @NatureofRequest WHERE ID = @ID" Dim cmd As New SqlClient.SqlCommand(sc1.CommandText) 'Create the sql command object and set its command type to execute the sql query to get the results sc1.Parameters.Add("@Status", SqlDbType.NVarChar, 50, "Status").Value = comboControl.Text sc1.Parameters.Add("@LastName", SqlDbType.NVarChar, 50, "LastName").Value = "@LastName" sc1.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50, "FirstName").Value = "@FirstName" sc1.Parameters.Add("@DateofRequest", SqlDbType.NVarChar, 50, "DateofRequest").Value = "@DateofRequest" sc1.Parameters.Add("@FromDate", SqlDbType.DateTime, 8, "FromDate").Value = "@FromDate" sc1.Parameters.Add("@ToDate", SqlDbType.DateTime, 8, "ToDate").Value = "@ToDate" sc1.Parameters.Add("@Days", SqlDbType.NVarChar, 50, "Days").Value = "@Days" sc1.Parameters.Add("@NatureofRequest", SqlDbType.NVarChar, 50, "NatureofRequest").Value = "@NatureofRequest" Dim myParm2 As SqlParameter = sc1.Parameters.Add("@ID", SqlDbType.Int, 4, "ID") myParm2.SourceVersion = DataRowVersion.Original sqlConn.Open() Try sc1.ExecuteNonQuery() Catch ex As SqlException MsgBox(ex.Message().ToString()) End Try sqlConn.Close() I know it has to do with the handling of variables, but can't narrow it down. Thank you, LWhite:)

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups