Solved

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

Posted on 2013-10-25
4
509 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 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now