?
Solved

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

Posted on 2013-10-25
4
Medium Priority
?
533 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
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
dustock earned 2000 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

850 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