Solved

Inserting  data to a table from gridview .

Posted on 2008-06-16
14
210 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

828 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