Link to home
Start Free TrialLog in
Avatar of fsuedu
fsuedu

asked on

Visual Studio Visual Basic Form load from sql database

I have the following code

Private Sub SettlementStatment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Me.SettlementTableAdapter.Fill(Me.NightAuditDataSet.Settlement)

 '---------------------------------------------------------------------------------------------------------------------------'
  Dim row As DataRow = NightAuditDataSet.Tables(0).Rows(NightAuditDataSet.Tables(0).Rows.Count - 1)
MessageBox.Show(row(0).ToString())
Me.Sitetxt.Text = 2

 End Sub

The fields are set up in the databindings properties at form[Desing]

but when you load the form the fields do not populate.

Even though the      MessageBox.Show(row(0).ToString())
Shows the last record number.

First time doing this --can you tell me the code for the sub
if I give you this
Server=FESSQL;Database=NightAudit;Trusted_Connection=yes;"
Table Name ----Settlement
Tried to make work -but had no luck

I'm I missing something relating to this:

myConnection = New SqlConnection(ConnStr)
            sql = "select * from tblSubCategories"
            da = New SqlDataAdapter(sql, myConnection)
            da.Fill(ds, "tblSubCategories")
            dg.DataSource = ds.Tables(0)


JK
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

I have a sample program that you can play with but, it is pretty large and I have a Access db as a test sample.
If you want I can email it to you.
Avatar of Sancler
Sancler

Your dataset/datatable is obviously being filled with something or you wouldn't get the last record number being reported.  So there seems to be something wrong with the bindings.  

You say "The fields are set up in the databindings properties at form[Desing]".  How was that done?  Did you drag stuff from the DataSource window onto the form?  Or did you drag controls from the ToolBox onto the form and then set the databindings by clicking the little buttons in the top right corner?

And can you please clarify the last bit of your post?  That seems to be dealing with a different table - "tblSubCategories" - rather than a table called "Settlement" which is (a) what your description "Table Name ----Settlement" refers to and (b) successfully brought over by the tableadapter.  Is the dg you refer to in that one of the controls for which "The fields are set up in the databindings properties at form[Desing]"?

Roger  
Avatar of fsuedu

ASKER

Hello Sancler

                          I  dragged controls from the ToolBox onto the form and then set the databindings by right clicking -go to properties-Data-(DataBindings)-Text-Then Select from the datasource the Appropriate sql field that matches the txt field?

No the last part was just an exampe -of code that I thought I might be missing

Thanks for your help
What does it say opposite the Text heading, under the DataBindings Heading, in the TextBox's Properties window?  If, as is quite likely, it refers to <someName>BindingSource what does it say under the headings DataSource and DataMember in the Properties window for that?  I would expect it to be NightAuditDataSet for the former and Settlement for the latter.

Roger
Avatar of fsuedu

ASKER

For exampe it say SettlementBindingSource - PreviouAdvDepositstxt  for the properties for txtbox PreviouAdvDepositstxtText  

Lost on where to go for the last part what does it say under the headings DataSource and DataMember in the Properties window for that?

Under the form[Design]

I have NightAuditDataSet ,SettlementBindingSource,SettlementTableAdapter

if I look at SettlementBindingSource properties in DataSource it has NightAuditDataSet

if that help?

Jk
SettlementBindingSource should also have a property DataMember.  It's pretty certain, given that the BindingSource is called SETTLEMENTBindingSource that that property will be Settlement.  If so, the databindings look like they should work.  

But if you can confirm that, it will close down one avenue of enquiry.

Can you try please changing the code that gets the number for your MsgBox to this

Dim row As DataRow = NightAuditDataSet.Tables("Settlement").Rows(NightAuditDataSet.Tables("Settlement").Rows.Count - 1)
MessageBox.Show(row(0).ToString())

and see if it's still giving the result you expect?

Roger
Avatar of fsuedu

ASKER

Good Morning

get this error message: Cannot bind to the property or column DateStamptxt on the DataSource.
Parameter name: dataMember
It's very difficult debugging things like this at a distance.  There's so many bits and pieces now scattered - by VB.NET 2005 - through so many files.  What I suggest you do is zip up the whole project and upload the .zip file here

http://www.ee-stuff.com/

then post the URL that you are given back in this thread.  That way, I can look at it as a whole rather than having to ask detailed questions.  If you want to remove any sensitive stuff - e.g. passwords in Connection Strings - before you do so, that will be OK.  Just replace them with *****, so I can recognise what's for real.

Roger
The good news is that I can find nothing wrong.  The bad news is that that gets you no nearer to solving your problem.

Although I have done a pretty through review of the code, I cannot actually run it to test without data.  I did dummy up a form and some data just to test out the settings on the DateStamptxt control but, as I expected from the code, the binding worked OK on that.

Originally, as I understood things, the form was loading OK and your message box was showing a value that confirmed that the data was loading, but all the controls were left blank.  With the error reported in your latest post, I would not even expect the form to load.  That error would occur on this line

        Me.DateStamptxt.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.SettlementBindingSource, "DateStamptxt", True))

in the InitializeComponent Sub.  Am I right about that?  If so, what changes did you make between the original position and that which you are now reporting?

Roger
Avatar of fsuedu

ASKER

You  might want to kill me I just relized it is pulling the data
from the SQL Table--but it is the first row instead of the last?

It is going off this
 Dim row As DataRow = NightAuditDataSet.Tables(0).Rows(NightAuditDataSet.Tables(0).Rows.Count - 1)
        MessageBox.Show(row(0).ToString())

Any Ideas

Avatar of fsuedu

ASKER

Its pulling off Primary Key 91 which is the first record now.
Put should be pulling off record primary key 98
but the message box says 98

Does that make sense?
When your form loads, the controls will display the data from the first record.  Your message box is - specifically - pulling its data from the last.  To move between the various records you will need some navigation mechanism, and I don't see any.  Just to check, stick a button on your form with this code

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SettlementBindingSource.Position += 1
    End Sub

and click it.  The controls should then update to reflect different records.

Roger
Avatar of fsuedu

ASKER

Yeap it worked the recordes changed as I hit the button until the last recored.

Is thier a way to make it the last record when it loads?
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fsuedu

ASKER

Thanks for all your help