Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

747 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

12 Experts available now in Live!

Get 1:1 Help Now