Maybe someone can help me with something that I have never been able to grasp, updating the datbase after changing data in a datagrid. I have the code below to load the datagrid and basically it takes a month of data and populates the datagrid. There are 2 combos above the data grid that the use has to select a month and a year and then it will populate. One combo is the month, the second the year. There are also 2 buttons, one for next month, the other for previous. I can go thru the data and it works flawlessly. I am now at the point where I need to figure out how to update the data if the user changes it. I am guessing I would use the dgCharges_CurrentCellChanged sub but not totally sure. Any help would be great. Heres the code.
VB Code:
  1. Dim sqlConn As SqlClient.SqlConnection
  2.     Dim sqlCmd As New SqlClient.SqlCommand
  3.     Dim sqlRead As SqlClient.SqlDataReader
  4.     Dim varNum As Integer = 0
  5.     Dim varTotalMon As Decimal = 0
  6.     Dim varUsage As Decimal = 0
  7.  
  8.     Private Sub LoadGrid()
  9.  
  10.         If cboMonth.Text = "Choose Month" Then
  11.         Else
  12.             If cboYear.Text = "Choose Year" Then
  13.             Else
  14.                 If varNum = 0 Then
  15.                     varTotalMon = 0
  16.                     varUsage = 0
  17.                     Dim daPubs As SqlDataAdapter
  18.                     Dim dsPubs As DataSet
  19.                     SqlConnection1.ConnectionString = "workstation id=""S-KG-CFSCE-2098"";packet size=4096;integrated security=SSPI;data s" & _
  20.                                 "ource=""SCE-SCE-DSVR02D"";persist security info=False;initial catalog=Telecomm"
  21.                     SqlConnection1.Open()
  22.                     sqlCmd.Connection = SqlConnection1
  23.                     sqlCmd.CommandType = CommandType.StoredProcedure
  24.                     sqlCmd.CommandText = "SPCharges"
  25.  
  26.                     sqlCmd.Parameters.Add("@month", SqlDbType.VarChar)
  27.                     sqlCmd.Parameters("@month").Value = cboMonth.Text
  28.                     sqlCmd.Parameters.Add("@year", SqlDbType.VarChar)
  29.                     sqlCmd.Parameters("@year").Value = cboYear.Text
  30.  
  31.                     daPubs = New SqlDataAdapter
  32.                     daPubs.SelectCommand = sqlCmd
  33.                     dsPubs = New DataSet
  34.                     daPubs.Fill(dsPubs)
  35.                     dgCharges.DataSource = dsPubs
  36.                     dgCharges.DataMember = "Table"
  37.  
  38.                     SqlConnection1.Close()
  39.  
  40.                     SqlConnection1.ConnectionString = "workstation id=""S-KG-CFSCE-2098"";packet size=4096;integrated security=SSPI;data s" & _
  41.                                 "ource=""SCE-SCE-DSVR02D"";persist security info=False;initial catalog=Telecomm"
  42.                     SqlConnection1.Open()
  43.                     Dim cmdtext As String = "Select * from Charges where Month like '" & cboMonth.Text & "' and Year like '" & cboYear.Text & "'"
  44.                     da.SelectCommand = New SqlCommand(cmdtext, SqlConnection1)
  45.                     Dim dr As SqlDataReader = da.SelectCommand.ExecuteReader
  46.                     While dr.Read
  47.                         If dr.IsDBNull(2) Then
  48.                         Else
  49.                             varTotalMon = varTotalMon + CDec(dr.GetValue(2))
  50.                         End If
  51.                         If dr.IsDBNull(3) Then
  52.                         Else
  53.                             varUsage = varUsage + CDec(dr.GetValue(3))
  54.                         End If
  55.                     End While
  56.                     txtMonthlyTot.Text = "$ " & CStr(varTotalMon)
  57.                     txtUsage.Text = "$ " & CStr(varUsage)
  58.                     dr.Close()
  59.                     SqlConnection1.Close()
  60.  
  61.  
  62.                     varNum = 1
  63.  
  64.                 Else
  65.  
  66.                     varTotalMon = 0
  67.                     varUsage = 0
  68.                     Dim daPubs As SqlDataAdapter
  69.                     Dim dsPubs As DataSet
  70.                     SqlConnection1.ConnectionString = "workstation id=""S-KG-CFSCE-2098"";packet size=4096;integrated security=SSPI;data s" & _
  71.                                                    "ource=""SCE-SCE-DSVR02D"";persist security info=False;initial catalog=Telecomm"
  72.                     SqlConnection1.Open()
  73.                     sqlCmd.Connection = SqlConnection1
  74.                     sqlCmd.CommandType = CommandType.StoredProcedure
  75.                     sqlCmd.CommandText = "SPCharges"
  76.  
  77.                     'sqlCmd.Parameters.Add("@month", SqlDbType.VarChar)
  78.                     sqlCmd.Parameters("@month").Value = cboMonth.Text
  79.                     'sqlCmd.Parameters.Add("@year", SqlDbType.VarChar)
  80.                     sqlCmd.Parameters("@year").Value = cboYear.Text
  81.  
  82.                     daPubs = New SqlDataAdapter
  83.                     daPubs.SelectCommand = sqlCmd
  84.                     dsPubs = New DataSet
  85.                     daPubs.Fill(dsPubs)
  86.                     dgCharges.DataSource = dsPubs
  87.                     dgCharges.DataMember = "Table"
  88.                     SqlConnection1.Close()
  89.                     SqlConnection1.ConnectionString = "workstation id=""S-KG-CFSCE-2098"";packet size=4096;integrated security=SSPI;data s" & _
  90.                                                     "ource=""SCE-SCE-DSVR02D"";persist security info=False;initial catalog=Telecomm"
  91.                     SqlConnection1.Open()
  92.                     Dim cmdtext As String = "Select * from Charges where Month like '" & cboMonth.Text & "' and Year like '" & cboYear.Text & "'"
  93.                     da.SelectCommand = New SqlCommand(cmdtext, SqlConnection1)
  94.                     Dim dr As SqlDataReader = da.SelectCommand.ExecuteReader
  95.                     While dr.Read
  96.                         If dr.IsDBNull(2) Then
  97.                         Else
  98.                             varTotalMon = varTotalMon + CDec(dr.GetValue(2))
  99.                         End If
  100.                         If dr.IsDBNull(3) Then
  101.                         Else
  102.                             varUsage = varUsage + CDec(dr.GetValue(3))
  103.                         End If
  104.                     End While
  105.                     txtMonthlyTot.Text = "$ " & CStr(varTotalMon)
  106.                     txtUsage.Text = "$ " & CStr(varUsage)
  107.                     dr.Close()
  108.                     SqlConnection1.Close()
  109.  
  110.                 End If
  111.  
  112.  
  113.             End If
  114.         End If
  115.     End Sub
  116.  
  117.     Private Sub cboYear_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboYear.SelectedIndexChanged, cboMonth.SelectedIndexChanged
  118.         LoadGrid()
  119.  
  120.  
  121.     End Sub
  122.  
  123.  
  124.     Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
  125.         If cboMonth.Text = "Choose Month" Or cboYear.Text = "Choose Year" Then
  126.             MsgBox("Month and Year must be chosen first", MsgBoxStyle.OKOnly, "Choose month and year first")
  127.         Else
  128.  
  129.             If cboMonth.SelectedIndex = 0 Then
  130.                 cboMonth.SelectedIndex = 11
  131.                 If cboYear.SelectedIndex = 0 Then
  132.                     cboMonth.SelectedIndex = 0
  133.                     MsgBox("No records before 2006", MsgBoxStyle.OKOnly, "No Records")
  134.                 Else
  135.                     cboYear.SelectedIndex = cboYear.SelectedIndex - 1
  136.                 End If
  137.             Else
  138.                 cboMonth.SelectedIndex = cboMonth.SelectedIndex - 1
  139.             End If
  140.         End If
  141.  
  142.     End Sub
  143.  
  144.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  145.         If cboMonth.Text = "Choose Month" Or cboYear.Text = "Choose Year" Then
  146.             MsgBox("Month and Year must be chosen first", MsgBoxStyle.OKOnly, "Choose month and year first")
  147.         Else
  148.  
  149.             If cboMonth.SelectedIndex = 11 Then
  150.                 cboMonth.SelectedIndex = 0
  151.                 If cboYear.SelectedIndex = 14 Then
  152.                     cboMonth.SelectedIndex = 11
  153.                     MsgBox("No records after 2020", MsgBoxStyle.OKOnly, "No Records")
  154.                 Else
  155.                     cboYear.SelectedIndex = cboYear.SelectedIndex + 1
  156.                 End If
  157.             Else
  158.                 cboMonth.SelectedIndex = cboMonth.SelectedIndex + 1
  159.             End If
  160.         End If
  161.     End Sub