Hi all,
I am quite new to Vb.NET. What I was trying to do is getting information from database table into a datagridview and save the changes back to database table.
I did the first step running. But when I make changes on the datagridview field and click "Save Changes" button, my message of "1 records updated" appears but all fields in the database changes to NULL values.
Can anyone see the problem on the code below. Please send your replies with details and examples as it is very hard for me to understand as I am a newbie
Thanks very much
Ceyhun
I am quite new to Vb.NET. What I was trying to do is getting information from database table into a datagridview and save the changes back to database table.
I did the first step running. But when I make changes on the datagridview field and click "Save Changes" button, my message of "1 records updated" appears but all fields in the database changes to NULL values.
Can anyone see the problem on the code below. Please send your replies with details and examples as it is very hard for me to understand as I am a newbie
Thanks very much
Ceyhun
**** This section is under Form1 class
Dim cn As New MySqlConnection("Data Source=localhost;Database=stokprogrami;User ID=root;Password=;")
Dim adapter As New MySqlDataAdapter
Dim command As New MySqlCommand
Dim dtListe As New DataTable()
**** This section adds the data to datagridview on change of a combobox
Private Sub cihazmodeli_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cihazmodeli.SelectedIndexChanged
'Datagridview'e girilir.
If Not CStr(cihazmodeli.SelectedItem.Value) = "" Then
Dim sorgu As String = "Select serinumarasi,garantisuresi,faturatarihi,servicetag,macid1,macid2,winlisans1,winlisans2 from stoklar where modelID='" & CStr(cihazmodeli.SelectedItem.Value) & "'"
command.CommandText = sorgu
command.Connection = cn
adapter.SelectCommand = command
adapter.Fill(dtListe)
mevcutcihazlar.DataSource = dtListe
cn.Close()
mevcutcihazlar.Columns(0).HeaderText = "Seri Numarasi"
mevcutcihazlar.Columns(1).HeaderText = "Garanti Süresi"
mevcutcihazlar.Columns(2).HeaderText = "Fatura Tarihi"
mevcutcihazlar.Columns(3).HeaderText = "Service Tag"
mevcutcihazlar.Columns(4).HeaderText = "Mac ID 1"
mevcutcihazlar.Columns(5).HeaderText = "Mac ID 2"
mevcutcihazlar.Columns(6).HeaderText = "Win Lisans 1"
mevcutcihazlar.Columns(7).HeaderText = "Win Lisans 2"
mevcutcihazlar.Visible = True
Else
mevcutcihazlar.DataSource = ""
mevcutcihazlar.Visible = False
End If
End Sub
**** this section is the event of click "Save Changes"
Private Sub savechanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savechanges.Click
If Not dtListe.GetChanges() Is Nothing Then
Dim update As New MySqlCommand("UPDATE stoklar SET serinumarasi = @serinumarasi, garantisuresi = @garantisuresi, faturatarihi = STR_TO_DATE(@faturatarihi,'%d.%m.%Y'), servicetag = @servicetag, macid1 = @macid1, macid2 = @macid2, winlisans1 = @winlisans1, winlisans2 = @winlisans2", cn)
update.Parameters.Add("@serinumarasi", MySqlDbType.VarChar, 255, "serinumarasi")
update.Parameters.Add("@garantisuresi", MySqlDbType.Int16, 3, "garantisuresi")
update.Parameters.Add("@faturatarihi", MySqlDbType.String, 255, "faturatarihi")
update.Parameters.Add("@servicetag", MySqlDbType.VarChar, 50, "servicetag")
update.Parameters.Add("@macid1", MySqlDbType.VarChar, 50, "macid1")
update.Parameters.Add("@macid2", MySqlDbType.VarChar, 50, "macid2")
update.Parameters.Add("@winlisans1", MySqlDbType.VarChar, 50, "winlisans1")
update.Parameters.Add("@winlisans2", MySqlDbType.VarChar, 50, "winlisans2")
adapter.UpdateCommand = update
cn.Close()
Dim guncellenen As Integer = adapter.Update(dtListe)
MessageBox.Show(guncellenen & " kayit güncellendi.")
End If
End Sub