Re: insert text data from datagrid in vb.net to sql server 2000

vb.net Cambodia
  • 13 years ago
    Hi JoginderKumar !
    pls try this code below.

    Public Const ConnectionString As String = _
    "integrated security=sspi;initial catalog=pubs;data source=(local)"
    Protected Const GetAllAuthorsSqlString As String = "select au_id, au_lname, au_fname, phone, address, city, state, zip, contract from authors order by au_lname, au_fname"


    Public Sub RefreshData()

    Dim connection As New SqlConnection(ConnectionString)
    connection.Open()

    Dim adapter As New SqlDataAdapter(GetAllAuthorsSqlString, connection)
    Dim dataset As New DataSet
    adapter.Fill(dataset)
    adapter.Dispose()

    connection.Close()

    Dim table As DataTable = dataset.Tables(0)
    AddHandler table.ColumnChanged, New DataColumnChangeEventHandler(AddressOf ColumnChanged)

    dataGridAuthors.DataSource = table

    End Sub


    Public Sub SaveChanges()

    Debug.WriteLine("SaveChanges Called")

    Dim table As DataTable = CType(dataGridAuthors.DataSource, DataTable)

    Dim changedRows As New ArrayList
    For Each row As DataRow In table.Rows
    Debug.WriteLine(row.RowState)
    If row.RowState <> DataRowState.Unchanged Then
    changedRows.Add(row)
    End If
    Next

    If changedRows.Count = 0 Then
    Return
    End If
    Dim connection As New SqlConnection(ConnectionString)
    connection.Open()
    Dim adapter As New SqlDataAdapter(GetAllAuthorsSqlString, connection)
    Dim builder As New SqlCommandBuilder(adapter)
    Dim rows() As DataRow = _
    CType(changedRows.ToArray(GetType(DataRow)), DataRow())
    adapter.ContinueUpdateOnError = True
    adapter.Update(rows)

    ' close...
    adapter.Dispose()
    connection.Close()



    End Sub

    sorry i am in the hurry.
    that code allow u reload data to datagrid and update data only when sth change in the datagrid.

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond