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

vb.net India
  • 13 years ago

    hi,

    Can anybody help me in sovling my problem related to datagrid in vb.net

    i want to write some text data in datagrid in vb.net during run time and it should save to sql server 2000. and when in need it should also be retrived from sql server2000 to vb.net end in my software.

    i tried a lot. pls help me as it is urgent.

    Thnks in advance

    JoginderKumar

  • 13 years ago

    Hi JoginderKumar !
    pls try this code below.

    and pls download this project http://www.planet-source-code.com/vb/scripts/ShowZip.asp?lngWId=10&lngCodeId=1585&strZipAccessCode=tp%2FA15858291

    in the url above will help you more.

    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.

  • 13 years ago

    Dear Anatha1,

    Thanks a Ton ! for ur kind reply and code.

    I will try this code and contact u if any problem persist.

    Again TKS a lot !!!!

    Joginder Kumar

Post a reply

Enter your message below

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.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann