Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 22nd, 2006, 03:13 AM
Authorized User
 
Join Date: Apr 2006
Location: Stockholm, , Sweden.
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default accessing table values in VB

Hi,

I have a GridView with several rows of data and I want this data to be inserted into a SQL Server Express table using VWD Express and VB.

The problem is that I don't know how to index the GridView. Here is what I have in my code behind page:

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim conn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationMa nager.ConnectionStrings("ekodbConnectionString").C onnectionString)
        Dim row As Integer
        Dim cust_ID As Integer
        Dim firstname As String
        Dim lastname As String
        For row = 1 To 10
            cust_ID = GridView1[row,1].Item 'Needs to be corrected!
            firstname = GridView1[row,2].Item 'Needs to be corrected!
            lastname = GridView1[row,3].Item 'Needs to be corrected!
            Dim cmd As SqlCommand = New SqlCommand("INSERT INTO customers VALUES (" & cust_ID & ", " & firstname & ", " & lastname & ")", conn)
            conn.Open()
            cmd.ExecuteScalar()
            conn.Close()
        Next
    End Sub

1. So, instead of the errorenous lines of code for retrieving the GridView values, what should I write?

Two additional questions:
2. In the code above, I stated For row = 1 To 10. Can I write While row <> nil or something to get everything?

3. I use cmd.ExecuteScalar, which works but is not really making sense as I don't try to get a value from the sub. Is there anything else that works to get the Sql going?

Thanks very much in advance for help with any of this!

Pettrer,Sweden


Coding is indeed a nine-to-five job; nine pm to five am.
__________________
Coding is indeed a nine-to-five job; nine pm to five am.
Reply With Quote
  #2 (permalink)  
Old April 22nd, 2006, 09:58 AM
Authorized User
 
Join Date: Apr 2006
Location: Stockholm, , Sweden.
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I got a reply in another forum (asp.net). I post it here and hope it's helpful to others.

Pettrer

============

Hi,

I think your problem is how to nagivate through the rows in your gridview.
You have to work out a few things:
1. Make sure you gave a ID for the bound controls in your aspx file say:
    lblCustID, lblFirstname, lblLastName - you will need these to refer to which label controls (asp:label) in the row you need to access.

2. You navigate the existing rows from top to bottom. If you need to access a certain record then you have to add another hidden column where you put a unique ID.

Here how it works:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim conn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationMa nager.ConnectionStrings("ekodbConnectionString").C onnectionString)




For Each gvr As GridViewRow In MyGridVie.Rows
      ' reference the labels in the row
      Dim cust_ID As String= CType(gvr.FindControl("lblCustID"), Label).Text
      Dim firstname As String=CType(gvr.FindControl("lblFirstName"), Label).Text
      Dim lastname As String=CType(gvr.FindControl("lblLastName"), Label).Text
         Dim cmd As SqlCommand = New SqlCommand("INSERT INTO customers VALUES (" & cust_ID & ", " & firstname & ", " & lastname & ")", conn)
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
Next

end sub

Since you are only inserting and not expecting a return value, you should use "ExecuteNonQuery"

I hope this helps.

Cheers,

Wilmar, KSA (Mabuhay!)


Coding is indeed a nine-to-five job; nine pm to five am.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing users profile values question? hcusto ASP.NET 2.0 Professional 2 August 19th, 2006 03:23 AM
Accessing the latest row in a table pramos.21d SQL Language 2 April 6th, 2006 06:56 PM
Accessing Parameter field values of sub report MyIssues_2005 Crystal Reports 0 July 14th, 2005 02:21 AM
Accessing values from the last sibling node graywoodhouse XSLT 3 October 29th, 2004 06:15 AM
Problem accessing table tlamazares SQL Server ASP 2 December 16th, 2003 04:03 PM



All times are GMT -4. The time now is 07:41 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.