This is my code:

Add_click

dbcmd = New SqlCommand("INSERT INTO dms(name, dept) VALUES (@name, @dept)", connection)

'Add Database Parameters
dbcmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name.Text
dbcmd.Parameters.Add("@dept", SqlDbType.VarChar).Value = dept.Text

But I want to add the data in gridview

Dim record_series As String = DirectCast(GridView1.FindControl("Record_Series_Number"), Label).Text
Dim doc_name As String = DirectCast(GridView1.FindControl("Document_Name"), Label).Text

Please help.. thanks in advance..:)

Recommended Answers

All 3 Replies

Private Sub FindUser()

        Dim objConnection As New SqlConnection _
            ("server=localhost;database= MSS;" + _
             "user id=sa;password=clement;")

        Dim objDataAdapter As New SqlDataAdapter( _
        "SELECT * FROM User WHERE state = '" + txtSearch.Text + "'", objConnection)
        Dim objDataSet As DataSet
        Dim objDataView As DataView
        Dim objCurrencyManager As CurrencyManager


        objDataSet = New DataSet()

        ' Fill the DataSet object with data...
        objDataAdapter.Fill(objDataSet, "User")

        ' Set the DataView object to the DataSet object...
        objDataView = New DataView(objDataSet.Tables("User"))

        ' Set our CurrencyManager object to the DataView object...
        objCurrencyManager = CType(Me.BindingContext(objDataView), CurrencyManager)

        Dim intPosition As Integer

        ' If the search field is not empty then...
        If txtSearch.Text <> "" Then
            objDataView.Sort = "state"

            ' Find the user_id...
            intPosition = objDataView.Find(txtSearch.Text)

            Dim objdatatable As DataTable = objDataSet.Tables("User")

            For Each myDataRow As DataRow In objdatatable.Rows

                fNameTextBox.Text = Convert.ToString(myDataRow("FirstName"))
                lNameTextBox.Text = Convert.ToString(myDataRow("LastName"))
                titleTextBox.Text = Convert.ToString(myDataRow("Title"))
                zoneTextBox.Text = Convert.ToString(myDataRow("zone"))
                stateTextBox.Text = Convert.ToString(myDataRow("State"))
                phoneTextBox.Text = Convert.ToString(myDataRow("Phone"))
                mailTextBox.Text = Convert.ToString(myDataRow("Mail"))
                snTextBox.Text = Convert.ToString(myDataRow("S/N"))
            Next

            With dtgSearchResult

                .AutoGenerateColumns = True
                .DataSource = objDataSet
                .DataMember = "Focals"

                ' Declare and set the alternating rows style...
                Dim objAlternatingCellStyle As New DataGridViewCellStyle()
                objAlternatingCellStyle.BackColor = Color.WhiteSmoke
                dtgSearchResult.AlternatingRowsDefaultCellStyle = objAlternatingCellStyle

            End With

        Else

            MessageBox.Show("please enter User's state", "User Search" _
                              , MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        If intPosition = -1 Then
            ' Display a message that the record was not found...

            tslAction.Text = "Record Not Found"
        Else
            ' Otherwise display a message that the record was
            ' found and reposition the CurrencyManager to that
            ' record...

            tslAction.Text = "Record Found"

            objCurrencyManager.Position = intPosition

        End If

    End Sub

this code block is to search a user data and display the result in a datagrid, see how you can adjust it to suit your use.....at least you will see where to add the data to the datagrid.

Thanks for the information chibex64..

Sorry.. I'm a newbie in programming..

How can this add data to sql from the gridview?
Actually, I have a gridview that has checkbox on it. When I tick a row, it automatically edit a row but I didn't save it on database.. I've used amit jain's code(csharpnetdotfreaks.blogspot.com)

I have different database for add_click
Ex.

cmd = New SqlCommand("INSERT INTO table1(name, dept) VALUES (@name, @dept)", connection)

How can I add gridview database on table1?
Ex.

Dim record As String = DirectCast(GridView1.FindControl("Record"), Label).Text
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.