update unbounded datagrid in database

jak

Member
Joined
Feb 21, 2009
Messages
19
Programming Experience
Beginner
hi evrybuddy,
i m newbi....please help me to save data of a datagridview to that particular datatable...i hav generated a datagrid by using unbound database and now i want to save that data into a particular table...this is my 1st project..so please guide me with code...i m using vb 2008, sqlserver 2005 :mad:
thanx in advance
 
If it's your first data projects you should have the Studio generate the data access code for you, that the best way to learn to create data applications in Visual Studio and where a beginner should start, try these:
Data Walkthroughs
Visual Basic How Do I Video Series (Forms over Data Video Series)
 
hello john...

sir..actually i want to save a datagrid, which i generated by this following code.....

Dim paymentno As String = txttotalnopayment.Text
Dim txtduedate As Date = Format(Now, "short date")
txtduedate = DateValue(DateTimePicker1.Text)
For i As Integer = 1 To paymentno
Dim item As New DataGridViewRow
item.CreateCells(DataGridView1)
With item
.Cells(0).Value = Val(txtagreement_no.Text)
.Cells(1).Value = i
.Cells(2).Value = DateAdd(DateInterval.Month, i, txtduedate)
.Cells(3).Value = Val(txtmonthlypayment.Text)
End With
DataGridView1.Rows.Add(item)


there r 3 textbox and 1 datetimepicker on a form and a datagridview...when i run this code,then i entered values in those textboxes and after clicking a button, datagrid is generated...now i want to save this generated datagridview into a table using a save button on the form...bt i dont hav idea how to do this....
i think now u understand what exactly i want to do...sir please help me with detail codes...:mad:
thanks 4 taking interest in a newbi problem...:D
 
I'd suggest adding your data to a DataSet and setting the DataGridView's DataSource to the DataSet. Once you've accomplished this you can insert the DataSet into the database.
 
VB.NET:
Dim dt As New DataTable

'Add columns to DataTable
dt.Columns.Add("ID", GetType(Integer)).AutoIncrement = True
dt.Columns.Add("AgreementNum", GetType(Int64))
dt.Columns.Add("CustomerName", GetType(String))

'Add rows to DataTable
dt.Rows.Add(Nothing, CInt(uxAgreementNum.Text), uxCustomerName.Text)
...

Me.DataGridView1.DataSource = dt

There may be some minor syntax errors here as I typed it freehand.
 
sir..actually i want to save a datagrid, which i generated by this following code.....
That's nice, but it would be a lot easier to delete everything you've written and watch the videos JohnH linked. While you may be upset that I'm asking you to trash a lot of effort and struggling in finding out things, there is a much better way

You want to uplaod to a DB, right?
So watch the vids and learn how to link your app to a db
Then you'll learn about datatables
Then you can simply drop a datatable and grid on your form, put data in it manually youself (with code simialr to what you have here, but better; access the datatable directly) and then upload it to the db, again using the instructions in the video

John's trying to start you on a path to doing your data access properly and easily.. I'd highly recommend following it
 
hello mattp,

actually i hav just generated that DGV by the given code and now i want to save data of this DGV to already existing transaction table....this DGV is not connected to any db, i just populated this by using this code....and agreement_no is not pk....by ur given code i tried that but still having no solution.......help me dear..:mad:
by the way thanx taking interest in a newbi stupid problem....
 
I know it's difficult to accept someone coming along after you seem to have made progress, and saying "It would be better to delete (nearly) everything you have done and start over" but I'm saying it from a position of experience and knowledge that you dont have.

Your approach to the problem is:
Somehow get the data out of the file into the program, then
Somehow get the data out of the program and into the db

My approach to the problem is:
Use the built in functionality of Visual Studio to link to the db, creating a datatable that can be uploaded automatically. (This code takes about 5 seconds to generate)
Get the data out of the file into the datatable that Visual Studio made for you, and then run the upload procedure


Can you see the difference? Treat this one like a practice run, learning how to read data from the file.. Ultimately though.. dump it and start over. Watch the videos, then you'll know how to solve the first part of "how cjard would approach the problem", nad then using your knowledge gained here, you can solve the second part

Also, take a read up on what MVC is, as a concept. VB.NET is NOT VB6. We don't put data into datagridviews; we use datagridviews to show data in datatables.

I strongly recommend that you do not spend hours trying to fit the somewhat poor code you have already written, to the task at hand. There is a much better solution to be had.

Watch the videos
Learn how to link the DB
Populate a datatable (NOT a datagridview) with the data
Call the routines to save the data into the db
 

Latest posts

Back
Top