?
Solved

Inserting  data to a table from gridview .

Posted on 2008-06-16
14
Medium Priority
?
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 

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

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

762 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