• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Inserting data to a table from gridview .

I have a grid with 7 columns. The third column is the unique /primary key but I hide it in the gridview1_rowcreated event.
First column is a an ItempTemplate with a checkbox. By placing a check in this checkbox indicates that this row has to be process. The second column (purchaseticket)is a checkbox where user will mark or not. By checking this column , it means that a purchaseticket is bought.
Column 3 to 5 are all bound columns/ fields to the 'Registration table.  Column 6 and 7 are dropdownlist.
Whatever is selected in these 2 dropdownlist , the text has to be written to the registration table.
My question is how would I loop to the grid and check for the rows  and INSERT it a  tblProcess.
If column cells 1 is check,that means that the row has to be written to teh database. But it has to check also if columnticket(purchasingticket column)is also slected. If it is , write this to the table ,otherwise not.

0
zachvaldez
Asked:
zachvaldez
  • 7
  • 7
1 Solution
 
JZoermanCommented:
You can loop through the rows and use FindControl to reference the object.
 Protected Sub CheckSelections()
        Dim Check1 As CheckBox
        Dim Check2 As CheckBox
 
        For I As Integer = 0 To GridView1.Rows.Count - 1
            Check1 = CType(GridView1.Rows(I).FindControl("CheckBox1"), CheckBox)
            Check2 = CType(GridView1.Rows(I).FindControl("CheckBox2"), CheckBox)
            If Check1.Checked = True And Check2.Checked = True Then
                'Both columns are checked for row I, do logic 
                MsgBox("Both are checked in row " & I)
            End If
        Next
    End Sub

Open in new window

0
 
zachvaldezAuthor Commented:
there are also 6 dropdownlist in addition too.
How would the INSERT syntax to sql then is coded? thanks
0
 
JZoermanCommented:
Here is a simple method for inserting data into SQL Server database as well as an example for retrieving the value(s) of DropDownList objects in a GridView. If you are using a default database with default connection string then this code will work as is. If not, must provide connection string in web.config to call. Note, this is not the only method, but a short and simple one.
Public Sub InsertNewRecord(ByVal Parameter1 As String, ByVal Parameter2 As String)
        Dim sql As New SqlDataSource()
        'Note: LocalSqlServer is a default 
        sql.ConnectionString = ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString()
        sql.InsertCommandType = SqlDataSourceCommandType.Text
 
        sql.InsertCommand = "Insert Into TableName (FieldName1, FieldName2) Values (@FieldName1, @FieldName2)"
        sql.InsertParameters.Add("FieldName1", Parameter1)
        sql.InsertParameters.Add("FieldName2", Parameter2)
        sql.Insert()
 
        sql.Dispose()
    End Sub
 
   Protected Sub CheckSelections()
        Dim Check1 As CheckBox
        Dim Check2 As CheckBox
        Dim DropDownList1 As DropDownList
        Dim DropDownList2 As DropDownList
 
        For I As Integer = 0 To GridView1.Rows.Count - 1
            Check1 = CType(GridView1.Rows(I).FindControl("CheckBox1"), CheckBox)
            Check2 = CType(GridView1.Rows(I).FindControl("CheckBox2"), CheckBox)
            DropDownList1 = CType(GridView1.Rows(I).FindControl("DropDownList1"), DropDownList)
            DropDownList2 = CType(GridView1.Rows(I).FindControl("DropDownList2"), DropDownList)
            If Check1.Checked = True And Check2.Checked = True Then
                'Both columns are checked for row I, do logic 
                MsgBox("Both are checked in row " & I & vbCrLf & "Dropdown list values are " & DropDownList1.SelectedValue & " and " & DropDownList2.SelectedValue)
            End If
        Next
    End Sub

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
zachvaldezAuthor Commented:
So..If I have boundfields in between those checkboxes and dropdownlists in the same row,, the INSert syntax should include them too within the loop.
0
 
JZoermanCommented:
If I understand you correctly, yes. Whatever data you are requiring from objects in the gridview to insert as a new record into another table need to be referenced in the loop for this scenario.
0
 
zachvaldezAuthor Commented:
So, how would I reference the boundfields? should I address them thru the cell locationas?
like for example the  PersonID that is in cell 3 and category n cell 4. many thnks for letting me understand better how the grid works.!
0
 
JZoermanCommented:
GridView1.Rows(Y).Cells(X).Text is one method that will work for label controls and may be the easiest.

Another option is to reference them the same way as the DropDownList and CheckBox controls. By converting a bound field to a template field will expose the type of control and its id.
0
 
zachvaldezAuthor Commented:
By converting a bound field to a template field will expose the type of control and its id.>>>
Right now it is bound with sqldatasource1 and the Grid with the fields configured manually in the design.
So if I change it to Templatefields, should I get rid of teh datasource(sqldatsource1).How will I reference the database then mapping to the fields? thanks for alternative solution!
0
 
JZoermanCommented:
When converting to a template field the current bindings will remain intact. The output will look exactly the same until or unless you modify the template field in some way. You still need a datasource and the one you are using will work just fine.

Using template fields opens an entirely new level of possibilities but is also a different topic.
0
 
zachvaldezAuthor Commented:
I think I will use this one.. GridView1.Rows(Y).Cells(X).Text

How is  Y and X referenced in the Gridview?-Hopefully this is my last question in this post. thanks
0
 
JZoermanCommented:
Replace Y with the index of the row and X with the index of the field. GridView1.Rows(0).Cells(0).Text to reference the first cell in the first row.
0
 
zachvaldezAuthor Commented:
GridView1.Rows(0).Cells(1).Text - First row,second column
GridView1.Rows(1).Cells(1).Text - Second row,second column
Is this right?
0
 
JZoermanCommented:
Yes
0
 
zachvaldezAuthor Commented:
THANKS FOR YOUR HELP!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now