Solved

VB.net 2010 creating and using a dataset and SQLdata adapter example

Posted on 2013-10-25
4
513 Views
Last Modified: 2013-10-25
Dear experts,

I need to use a dataset to add or update update a table with about 100 000 recs from a SQL 2008 server.

Can you please assitance to my example and show me how to complete complete this approach to view data and modify data?

Dim conn As New SqlConnection
        conn.ConnectionString = My.Settings.XXXX      
 conn.Open()
        Dim ds As New DataSet("Claims")
        Dim cmd As New SqlCommand("Select * from claim where claim.claimID ='" & txtClaimId.Text & "'", conn)
        Dim da As New SqlDataAdapter
        da.SelectCommand = cmd
        da.Fill(ds)
        txtCompany
       txtCompName
        txtloc......


Thanks
Cj
0
Comment
Question by:cin_champ
[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
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
dustock earned 500 total points
ID: 39600604
This should get you started

        Dim cnSQL As SqlConnection = Nothing
        Dim cmdSQL As SqlCommand
        Dim daSQL As SqlDataAdapter
        Dim dsData As DataSet = Nothing

        cnSQL = New SqlConnection()
        cnSQL.ConnectionString = YOURCONNECTIONSTRING

        Try
            cnSQL.Open()
        Catch ex As Exception
            cnSQL.Dispose()
            cnSQL = Nothing
        End Try

        cmdSQL = New SqlCommand()
        cmdSQL.Connection = cnSQL
        cmdSQL.CommandText = 'YOUR SELECT STATEMENT
        cmdSQL.CommandType = CommandType.Text

        dsData = New DataSet()
        Try
            daSQL = New SqlDataAdapter(cmdSQL)
            daSQL.Fill(dsData)
            daSQL.Dispose()
        Catch ex As Exception
            dsData.Dispose()
            dsData = Nothing
        Finally
            cmdSQL.Dispose()
            cnSQL.Close()
            cnSQL.Dispose()
        End Try

        Return dsData

Open in new window

0
 

Author Comment

by:cin_champ
ID: 39600645
Dustock ,

thanks for the response but  need to know how to I link my text boxes to the dsData?

txtCompany = ?
txt customername = ?

Thanks
Cj
0
 
LVL 9

Assisted Solution

by:dustock
dustock earned 500 total points
ID: 39601129
Sorry about that, I thought you had that part covered.  Just an FYI, the code above was intended to be a subroutine, and actually the connection could be its own subroutine, but we wont get into that right now.  If you aren't putting the above code in a subroutine remove the 'return dsData" and replace with the code below.

In the example below the (0)(0) is first row, first column and (0)(1) is first row, second column.  The commented out the lines do the exact same thing, but I replaced Tables(0) with Tables("tablename"), and I guessed at the database column names after the row(0).  

Let me know if I can be of anymore help.

        If dsData.Tables(0).Rows.Count > 0 Then
            txtCompany.Text = dsData.Tables(0).Rows(0)(0).ToString();
            txtcustomername.Text = dsData.Tables(0).Rows(0)(1).ToString();
            'txtCompany.Text = dsData.Tables("claim").Rows(0)("Company").ToString();
            'txtcustomername.Text = dsData.Tables("claim").Rows()("Name").ToString();
        Else
            MessageBox.Show("No data returned from query")
        End If

Open in new window

0
 

Author Closing Comment

by:cin_champ
ID: 39601456
Thank You!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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