Updating Records

frankwhite

Well-known member
Joined
Nov 30, 2004
Messages
49
Programming Experience
Beginner
Hi, the following coding works on a form where I want information entered in a textbox to update in a Datagrid.

[FONT=&quot][/FONT]
VB.NET:
 Dim Neworder As DataRow = DsItems1.Tables("Items").NewRow()
    Neworder("Description") = TextBox1.Text
    Neworder("Quantity") = TextBox2.Text
    DsItems1.Tables("Items").Rows.Add(Neworder)

It works great apart from the fact I want my datagrid to be on another form. I have a "Load" button on the another form which I know how to work and it also contains the datagrid, I want the information entered here to be filling up the datagrid on that page. Is this possible? Thanks
 
yep.. just get that datagrid to sing from the same song sheet (datasource) as this one
 
What Cjard is saying is that, if your datagrid on your other form has the same datasource as the textbox then the changes will be automatic.
 
Oh i understand partly, anyway what I have done is added 2 datagrids turned the visibility of the first one to false, and when the load button is pressed on the second form it works. However when i press the add records button twice it breaks on the following line:

DsItemsGivenOut1.Tables("ItemsGivenOut").Rows.Add(Addorder)

Do you have any ideas why it might be doing this?
 
Sorry it wasnt that line it was the following line:

OleDbDataAdapter1.Update(DsItemsGivenOut1)

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll.

You see the above line is in a button on the first form which I use to update the records. (DSItemsGivenOut1) is used on both forms.
 
Pressing Add twice normally throws an error in the situation where you have set at least one field to have a rule of "Cannot be Null"

Press Add, null entries are added and youre suppoed to edit them,
Dont edit them press Add again and it crashes when it tries to commit the first lot of null values into the datatable

PLEASE dont post the 'error' message "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll."

Its about as useless as a chocolate teapot, and surely you must be able to see this too! We cant read minds and we cant remote debug your code, so please investigate the exception more and post the real error message in future!
 
Oh i understand partly, anyway what I have done is added 2 datagrids turned the visibility of the first one to false, and when the load button is pressed on the second form it works.

Huh? why dont you just make a constructor parameter on form 2 be the dataset, have form1 pass the populated dataset into form 2 and then the form2 datagrid can be set to use that as its dataource

-> "singing from the same song sheet" is an english phrase. google it?!
 
Huh? why dont you just make a constructor parameter on form 2 be the dataset, have form1 pass the populated dataset into form 2 and then the form2 datagrid can be set to use that as its dataource

Constructor parameter? I understand where you sed pass the dataset from form1 into form2, and the grid can show the updated data. But the first part has confused me a little bit.

Thanks for your info on the add part, oh im sorry about the stupid error message i posted. Well the coding works sometimes and at other times it breaks on that line.
 
Constructor parameter?
The Sub New method is all objects constructor, from where the object is created. You will find the term Constructor used in the documentation very often since it is very important and common to create programming objects.
VB.NET:
Public Sub New(ByVal parameter As DataSet)
    DataGriddd.DataSource = parameter
End Sub
 
John thanks for that, I have understood what you have said. However when CJard said pass the pupulated dataset onto form 2, how would I go about this? Sorry I know i might sound abit dumb, its just im at the early stages. :(

I have used your coding on my form 2

Public Sub New(ByVal parameter As DataSet)
DataGrid1.DataSource = parameter
End Sub

On my first form the following is some of the coding I use to add the text into the datagrid:

Dim Neworder As DataRow = DsItems1.Tables("Items").NewRow()
Neworder("Description") = TextBox1.Text
Neworder("Quantity") = TextBox2.Text
DsItems1.Tables("Items").Rows.Add(Neworder)

Im sure the answer is facing me in the face.
 
frankwhite, you need to start thinking about it in more simple terms. There is no mystery and magic to programming code, despite what the word code suggests. You have Form1 and Form2 right? So, Form1 has something that you want to give to Form2. So the constructor for Form2 (Sub New) should be like this...

VB.NET:
Public Sub New (Byval Ds as Dataset)
 
End sub

Then when you create your instance of Form2..

VB.NET:
Dim Frm2 as new Form2(Your Dataset)

So you have now passed your datasource as a parameter to Form2.

There is a chap on these forums called Jmcilhinney he has a tutorial in his signature called 'Multiple Forms' check it out.
 
Thanks, ive checked that page on the multiple forms and it helped me out a little bit. The way you broke it down was good thanks alot, logically let me have a go. Basically information must be sent from form 1 to form 2, so on form 2 I have the following at the top:

Public Sub New(ByVal DsItemsGivenOut1 As DataSet)
End Sub

On Form1 I have

Dim Frm2 as new Form2(DsItemsGivenOut1)

Private Sub btnload_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnload.Click
OleDbDataAdapter1.Update(DsItemsGivenOut1)
frm2.Show()
Me.Hide()
End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Addorder As DataRow = DsItemsGivenOut1.Tables("ItemsGivenOut").NewRow()
Addorder("Name") = TextBox1.Text
Addorder("Dept") = TextBox2.Text
DsItemsGivenOut1.Tables("ItemsGivenOut").Rows.Add(Addorder)
End Sub


I know im going wrong somewhere, but its frustrating not knowing where. I know its some simple problem where im lacking on, if you could help me out in any way I would appreciate it alot. Thanks

What you showed me in your last post was probably the most simpliest way but I still dont know why its not working, is it possible if you could make a sample program I could have a look at please?
 
Back
Top