update ข้อมูลจาก datagridview ลงดาต้าเบส แต่มันขึ้น error ว่า Object reference not set to an instance of an object. แก้ยังงัยค่ะ
Code (VB.NET)
Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
check_radio()
If DataGridView1.RowCount <= 0 Then Exit Sub
Dim countDVG1 As Integer = 0
For i As Integer = DataGridView1.RowCount - 1 To 0 Step -1
If IsDBNull(DataGridView1.Rows(i).Cells("custpay").Value) Or IsDBNull(DataGridView1.Rows(i).Cells("discount").Value) Then
countDVG1 = countDVG1 + 1
End If
Next
If rdoBB.Checked = False And rdoKB.Checked = False And rdoTMB.Checked = False Then
MessageBox.Show(" กรุณาเลือก เลขที่บัญชีการโอน ", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Stop)
ElseIf txtpay.Text = "" Then
MessageBox.Show(" กรุณาระบุจำนวนยอดชำระ ", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Stop)
ElseIf countDVG1 > 0 Then
MessageBox.Show(" กรุณากรอกระบุยอดชำระและหมายเหตุ ให้ครบถ้วน ", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Stop)
ElseIf lbltotalpay.Text <> txtpay.Text Or lbltotalRemain.Text <> lblramain.Text Then
MessageBox.Show(" ยอดเงินในช่องชำระไม่ตรงกับยอดเงินโอนรวม ", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Else
If MessageBox.Show(" คุณต้องการที่จะบันทึกข้อมูลนี้หรือไม่ ? ", "บันทึก", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
'Dim Gen_billno As String
Dim billnumberDGV As String
Dim payDGV As Decimal
Dim remainDGV As Decimal
Dim choieDGV As String
Dim remarkDGV As String
timetick = DateTimePicker2.Value
Try
conjen = New SqlConnection(strcon)
comjen = New SqlCommand("select MAX(billNo) from " & sale_name & "", conjen)
dajen = New SqlDataAdapter(comjen)
dajen.Fill(dsjen, "data")
jen = CInt(dsjen.Tables("data").Rows(0)(0)) + 1
Catch ex As Exception
jen = 11111
End Try
billreport = jen
For j As Integer = DataGridView1.RowCount - 1 To 0 Step -1
billnumberDGV = DataGridView1.Rows(j).Cells("billnumber").Value
payDGV = Convert.ToDecimal(DataGridView1.Rows(j).Cells("custpay").Value)
remainDGV = Convert.ToDecimal(DataGridView1.Rows(j).Cells("discount").Value)
If IsDBNull(DataGridView1.Rows(j).Cells("choice_remake").Value) Then
choieDGV = ""
Else
choieDGV = DataGridView1.Rows(j).Cells("choice_remake").Value
End If
If IsDBNull(DataGridView1.Rows(j).Cells("remark").Value) Then
remarkDGV = ""
Else
remarkDGV = DataGridView1.Rows(j).Cells("remark").Value
End If
'Dim tr As SqlTransaction = consave.BeginTransaction
'Try
'comsave = New SqlCommand("UPDATE pu SET checkbox = @checkbox , billno = @billno , cashtotal = @cashtotal , datemake =@datemake , billtotal = @billtotal ,arrears = @arrears,choice_remake = @choice_remake , remark =@remark ,datetrasfer = @datetrasfer , bank = @bank , pay = @pay WHERE billnumber = '" & app.Cells.Item("billnumber").Value & "' ", consave)
'Dim tr As SqlTransaction = consave.BeginTransaction
comsave = New SqlCommand("UPDATE " & sale_name & " SET checkbox = @checkbox , billno = @billno , payment = @payment , datemake =@datemake , transfertotal = @transfertotal ,billtotal= @billtotal ,discount = @discount,datetrasfer = @datetrasfer ,bank = @bank , checknumber = @checknumber , pay = @pay WHERE billnumber = '" & billnumberDGV & "' ", consave)
comsave.Parameters.AddWithValue("@checkbox", "2")
comsave.Parameters.AddWithValue("@billno", jen)
comsave.Parameters.AddWithValue("@payment", payDGV.ToString("###,###,##0.00"))
comsave.Parameters.AddWithValue("@datemake", Now.ToString())
comsave.Parameters.AddWithValue("@transfertotal", lbltotalpay.Text)
comsave.Parameters.AddWithValue("@billtotal", sum.ToString("###,###,##0.00"))
comsave.Parameters.AddWithValue("@discount", remainDGV.ToString("###,###,##0.00"))
comsave.Parameters.AddWithValue("@datetrasfer", timetick)
comsave.Parameters.AddWithValue("@checknumber", checknumber)
comsave.Parameters.AddWithValue("@bank", bank)
comsave.Parameters.AddWithValue("@pay", payby)
consave.Open()
comsave.ExecuteNonQuery()
consave.Close()
'tr.Commit()
'Catch ex As Exception
'tr.Rollback()
'MessageBox.Show(" บันทึกข้อมูลไม่ได้ ", "บันทึก", MessageBoxButtons.OK, MessageBoxIcon.Information)
'End Try
Next
MessageBox.Show(" บันทึกข้อมูลเรียบร้อยเเล้ว ", "บันทึก", MessageBoxButtons.OK, MessageBoxIcon.Information)
DataGridView1.Rows.Clear()
'DataGridView1.Hide()
frmReport.Show()
End If
End If
End Sub
มันขึ้น Error ตรง consave.Open() ว่า
Object reference not set to an instance of an object.
จะแก้ยังงัยอ่ะค่ะ รบกวนผู้รู้ด้วยค่ะ ขอบคุณคร้าTag : .NET, VB.NET
Date :
2012-09-06 16:16:54
By :
pukbung
View :
1299
Reply :
1
Date :
2012-09-06 16:23:05
By :
pukbung
Load balance : Server 04