Hello people i'm a newbie programmer of vb.net, so i have this problem so far, i am searching for a related answer but found no luck, so i decided to post my problem.
so here it is.
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim connstr As String = "server=midtelephone\sqlexpress; database=testdb; user= sa; password=sa;"
cmdconn = New SqlConnection
cmd = New SqlCommand
cmdconn.ConnectionString = sqlstr
cmd.Connection = cmdconn
cmdconn.Open()
Dim period, VOUCH_AMT, INDIVIDUAL_AMT, check_no, D_MAILED, DIR_NO As String
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
'cmd.CommandText = "insert into tobee.EBD_BILLHISTORY(period, vouch_amt, individual_amt, check_no, d_mailed, dir_no) values" &_
'" (@period,@VOUCH_AMT,@INDIVIDUAL_AMT,@check_no,@D_MAILED,@DIR_NO)"
period = Me.DataGridView1.Rows(i).Cells(0).Value()
VOUCH_AMT = Me.DataGridView1.Rows(i).Cells(1).Value()
INDIVIDUAL_AMT = Me.DataGridView1.Rows(i).Cells(2).Value()
check_no = Me.DataGridView1.Rows(i).Cells(3).Value()
D_MAILED = Me.DataGridView1.Rows(i).Cells(4).Value()
DIR_NO = Me.DataGridView1.Rows(i).Cells(5).Value()
cmd.CommandText = "insert into tobee.EBD_BILLHISTORY(period, vouch_amt, individual_amt, check_no, d_mailed, dir_no)values" & _
"('" & period & "','" & VOUCH_AMT & "','" & INDIVIDUAL_AMT & "','" & check_no & "','" & D_MAILED & "', '" & DIR_NO & "')"
cmd.ExecuteNonQuery()
MsgBox("Saved")
Next
cmdconn.Close()
End Sub
i'd like to update my sql database through datagridview.
( that datagridview was called from another sql query - cmd.CommandText = " select period, VOUCH_AMT, INDIVIDUAL_AMT, check_no, D_MAILED, DIR_NO from tobee.EBD_BILLHISTORY where CLAIM_NO like '" + claimno.ToString + "'"select period, VOUCH_AMT, INDIVIDUAL_AMT, check_no, D_MAILED, DIR_NO from tobee.EBD_BILLHISTORY where CLAIM_NO like '" + claimno.ToString + "'")
now the problem is/are, the error/s come up when i click the save button. whenever i click the save button leaving the other columns empty. errors shows up "invalidcastexception was unhandled" (on a certain rows that is empty) -Conversion from type 'DBNull' to type 'String' is not valid.
aaaaaand.. another error comes up when inserting different value in each rows. specially when i want to insert a datetime value. not really familiar with the code.
is there something wrong with my code? all processes are good(like pulling up the data) except clicking the save button. or updating my database. thanks in advance. your replies are much appreciated.