Solved

Inserting  data to a table from gridview .

Posted on 2008-06-16
14
207 Views
Last Modified: 2013-11-26
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
Comment
Question by:zachvaldez
  • 7
  • 7
14 Comments
 
LVL 2

Expert Comment

by:JZoerman
ID: 21803189
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
 

Author Comment

by:zachvaldez
ID: 21804209
there are also 6 dropdownlist in addition too.
How would the INSERT syntax to sql then is coded? thanks
0
 
LVL 2

Accepted Solution

by:
JZoerman earned 500 total points
ID: 21804819
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
 

Author Comment

by:zachvaldez
ID: 21804936
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
 
LVL 2

Expert Comment

by:JZoerman
ID: 21805123
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
 

Author Comment

by:zachvaldez
ID: 21810033
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
 
LVL 2

Expert Comment

by:JZoerman
ID: 21810202
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:zachvaldez
ID: 21812690
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
 
LVL 2

Expert Comment

by:JZoerman
ID: 21813548
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
 

Author Comment

by:zachvaldez
ID: 21815324
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
 
LVL 2

Expert Comment

by:JZoerman
ID: 21815534
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
 

Author Comment

by:zachvaldez
ID: 21818798
GridView1.Rows(0).Cells(1).Text - First row,second column
GridView1.Rows(1).Cells(1).Text - Second row,second column
Is this right?
0
 
LVL 2

Expert Comment

by:JZoerman
ID: 21818822
Yes
0
 

Author Comment

by:zachvaldez
ID: 21819563
THANKS FOR YOUR HELP!!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now