473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Import data from Datagrid to sqlserver in vb.net 2003

Hello ,

Is there any way i can import data from datagrid to sqlserver.

please advise .

thanks & Regards

Oct 25 '07 #1
1 4002
Not sure if VB2003 can do this, but in VB2005 the dataset object has a
CreateDataReader property for passing data from a .Net dataset table to sql
server. Here is the code for VB2005 (might work in VB2003)

First: you need a destination table on your sql server DB. The table
structure needs to match the .Net table in your app that the datagridview is
pulling from. If the data in the datagridview is not based on a data table,
then you need to loop through the datagridview (VB2005 - datagrid in VB2003)
and populate a datatable contained in a dataset. Once you have a data table
which is contained in a dataset and also a corresponding destination table on
the server DB -- you can do this the following using a sqlDataAdapter:

'--just getting the table structure into memory here
da.SelectCommand.CommandText = "Select * From serverTable"
da.Fill(ds, "serverTbl")

da.InsertCommand.CommandText = "Insert Into ServerTable(rowID, sName, BDay)
Select @rowID, @sName, @BDay"
da.InsertCommand.Parameters.Add("@rowID", SqlDbType.Int, 4, "rowID")
da.InsertCommand.Parameters.Add("@sName", SqlDbType.varchar, 50, "sName")
da.InsertCommand.Parameters.Add("@BDay", SqlDbType.datetime, 8, "BDay")

Dim reader As DataTableReader = ds.Tables("localAppTbl").CreateDataReader
ds.Tables("serverTbl").Load(reader, LoadOption.Upsert)
da.Update(ds, "serverTbl")

this statement
-->>ds.Tables("serverTbl").Load(reader, LoadOption.Upsert)
is where the transfer of data occurs from your local .Net dataTable to the
destination table on the server DB. Note the arg -- LoadOption.Upsert
Upsert is the LoadOption that worked for me. Then of course, you have to call
-->>da.Update(ds, "serverTbl")
to write the data from memory to the destination table on the disk.

If VB2003 doesn't support this code, you can always loop through your local
..Net dataTable -- this requires you to manually open the sql connection
object.

If conn1.State = ConnectionState.Closed Then conn1.Open()
For Each dr As DataRow In ds.Tables("yourLocalTbl").Rows
da.SelectCommand.CommandText = "Insert Into ServerTable Select " &
dr("rowID").Value.ToString & ", '" & dr("sName").Value.ToString & ", '" &
dr("BDay").Value.ToString & "'"
da.SelectCommand.ExecuteNonQuery
Next
conn1.Close

Regards,
Rich
"Amit" wrote:
Hello ,

Is there any way i can import data from datagrid to sqlserver.

please advise .

thanks & Regards

Oct 25 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alan Z. Scharf | last post by:
Win Server 2003 VS.Net 2003 --------------- 1. I'm having the same problem below on all six of my pages with a datagrid item. 2. These pages all worked fine for months until problem started....
1
by: pmud | last post by:
I need an editable datagrid. But for editing a datagrid as shown in the walkthrough: Using a DataGrid Web Control to Read and Write Data, the table in the SQL database should have a primary key....
4
by: Price Brattin | last post by:
I dragged a datagrid onto a VB.Net WinForm and used the properties page and a few lines of code to set it up, including the DataSource. No wizard setup was used. It works fine except for one...
3
by: Scottie_do | last post by:
I have a 20meg in-memory stack-based array and I'd like to normalise in the client's memory... then add foriegn keys, and display results on a datagrid. I discovered that converting the stack to a...
0
by: Jer | last post by:
I am using TransferSpreadsheet from an ADP file to import an Excel spreadsheet into an MSDE database. On one computer, when I run the code to import the spreadsheet, it prompts for an SQLServer...
1
by: Cylix | last post by:
I am going to show database table to a datagrid. I try to select the table to a sqlDataReader, but I found it is not straight to show it to the datagrid. It needs to convert the sqldatareader to a...
4
by: Rick | last post by:
I've moved code from a stage machine to the production machine, exact same code works fine on the stage machine, they are both windows 2003 servers, I'm getting a "Cannot generate SSPI context"...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
7
by: bobdurie | last post by:
I'm trying to run an import of some data that has unix style carriage returns throughout (ie, 0x0A, or \n). When i use osql to import the data, it shows up in the SQL Server 2005 database as 0x0D...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.