Datagridview update

foradream

New member
Joined
Feb 8, 2010
Messages
2
Programming Experience
1-3
my datagridview load code

VB.NET:
 Private Sub frm_kullanicilar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        DataGridView1.DataSource = kisi.GetDataset
        DataGridView1.DataMember = "kisiler"

    End Sub

VB.NET:
my dataset get code
    Public Function GetDataset() As DataSet
        Dim ds As DataSet = New DataSet
        Dim da As SqlDataAdapter = con.ExecAdapter("select * from kisiler")
        da.Fill(ds, "kisiler")
        Return ds
    End Function

my dataset update class
VB.NET:
      Public Sub UpdateDataset(ByVal ds As DataSet)


        Dim sqlUpdate As String = "Update (kisiler) SET Kisi_adi = @p2, Kisi_soyadi = @p3, Kisi_Tel = @p4, Kisi_Derece = @p5, Kisi_adres = @p6, Kisi_kayit_tarihi = @p7 WHERE (Kisi_ID = @p1)"
        Dim sqlDelete As String = "DELETE FROM Kisiler WHERE (Kisi_ID = @p1)"
        Dim sqlInsert As String = "INSERT INTO Kisiler (Kisi_adi, Kisi_soyadi, Kisi_Tel, Kisi_Derece, Kisi_adres, Kisi_kayit_tarihi) VALUES (@p1,@p2,@p3,@p4,@p5,@p6)"


        Dim cmdInsert As SqlCommand = con.ExecCommand(sqlInsert)
        cmdInsert.Parameters.Add("@p1", SqlDbType.Int, 50, "kisi_ID")
        cmdInsert.Parameters.Add("@p2", SqlDbType.VarChar, 50, "Kisi_adi")
        cmdInsert.Parameters.Add("@p3", SqlDbType.VarChar, 50, "Kisi_soyadi")
        cmdInsert.Parameters.Add("@p4", SqlDbType.VarChar, 50, "Kisi_tel")
        cmdInsert.Parameters.Add("@p5", SqlDbType.VarChar, 50, "Kisi_derece")
        cmdInsert.Parameters.Add("@p6", SqlDbType.NText, "Kisi_Adres")
        cmdInsert.Parameters.Add("@p7", SqlDbType.Date, "Kisi_kayit_tarihi")

        Dim cmdDelete As SqlCommand = con.ExecCommand(sqlDelete)
        cmdDelete.Parameters.Add("@p1", SqlDbType.Int, 50, "kisi_ID")

        Dim cmdUpdate As SqlCommand = con.ExecCommand(sqlUpdate)
        cmdUpdate.Parameters.Add("@p1", SqlDbType.Int, 50, "kisi_ID")
        cmdUpdate.Parameters.Add("@p2", SqlDbType.VarChar, 50, "Kisi_adi")
        cmdUpdate.Parameters.Add("@p3", SqlDbType.VarChar, 50, "Kisi_soyadi")
        cmdUpdate.Parameters.Add("@p4", SqlDbType.VarChar, 50, "Kisi_tel")
        cmdUpdate.Parameters.Add("@p5", SqlDbType.VarChar, 50, "Kisi_derece")
        cmdUpdate.Parameters.Add("@p6", SqlDbType.NText, "Kisi_Adres")
        cmdUpdate.Parameters.Add("@p7", SqlDbType.Date, "Kisi_kayit_tarihi")

 Dim da As SqlDataAdapter = New SqlDataAdapter
        da.InsertCommand = cmdInsert
        da.UpdateCommand = cmdUpdate
        da.DeleteCommand = cmdDelete
        da.Update(ds, "kisiler")
        ds.AcceptChanges()
    End Sub



my dataset update button
VB.NET:
   Private Sub Kaydet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Kaydet.Click
        kisi.UpdateDataset(DirectCast(DataGridView1.DataSource, DataSet))

    End Sub

i get Conversion from string "Kisi_Adres" to type 'Integer' is not valid.
Why is trying to convert i dont under stand

my datagridview columns
mu731y.jpg
 
There are 4 (5 actually, but one is obsolete) overloaded Add methods of the SqlParameterCollection Class (the cmdInsert.Parameters property is of type SqlParameterCollection). They are:
Add(SqlParameter) - Adds the specified SqlParameter object to the SqlParameterCollection.
Add(String, SqlDbType) - Adds a SqlParameter to the SqlParameterCollection given the parameter name and the data type.
Add(String, Object) - Obsolete. Adds the specified SqlParameter object to the SqlParameterCollection.
Add(String, SqlDbType, Int32) - Adds a SqlParameter to the SqlParameterCollection, given the specified parameter name, SqlDbType and size.
Add(String, SqlDbType, Int32, String) - Adds a SqlParameter to the SqlParameterCollection with the parameter name, the data type, and the column length.

This line in your code: cmdUpdate.Parameters.Add("@p6", SqlDbType.NText, "Kisi_Adres")
is of the form Add(String, SqlDbType, String) which is not valid as one of the overloaded methods. The framework is expecting an Integer type as the third parameter.

This is just my two cents: why write all this code when the data wizards will do it for you? Would you ever create a form design without the designer?
 
Thank you for your explanation. why i write this code? because I want to learn this way. and i dont create any form withour the designer.
 

Latest posts

Back
Top