471,120 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Retriveing a value from a dataset - VB.NET

Hello

I am new to VB.NET, so sorry if this is really obvious.

I have written the following code to fill my dataset from a stored procedure requiring 1 paramter input (from a SQL database). The Stored Procedure is quite lengthy including a number of inner joins.

The Dataset is used to populate a combobox on a form. I get the combobox to be populated by the dataset correctly based on my query selection. When the combobox selected item is changed, I want to return a value from the Dataset based on a row number (corresponding to the SelectedIndex from my ComboBox) given a Column Name within the dataset.

However, when I get to the SelectedIndexChange procedure the dataset is nothing, even though I have declared the variable as private within the form class (i.e. not within the set up procedure which creates the Dataset).

Code within initial procedure on loading the form:
Expand|Select|Wrap|Line Numbers
  1.             Dim OLEConnect As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(genCONNECTION)
  2.             Dim cmd As OleDbCommand = New OleDbCommand("sp_get_permissioned_single_curves", OLEConnect)
  3.             cmd.CommandType = CommandType.StoredProcedure
  4.             Dim InputValue As OleDbParameter = cmd.Parameters.Add("@curvename", OleDbType.VarChar, 50)
  5.             InputValue.Direction = ParameterDirection.Input
  6.             InputValue.Value = PersistedObjects.LoginUserName
  7.  
  8.             Dim daCurveSetup As OleDbDataAdapter = New OleDbDataAdapter()
  9.             daCurveSetup.SelectCommand = cmd
  10.  
  11.             Dim dsCurveSetup = New DataSet("CurveSetup")
  12.  
  13.             daCurveSetup.Fill(dsCurveSetup, "CurveSetup")
  14.  
  15.             'Add Data to the Curve Combo Box:
  16.             With cmbSingleCurvename
  17.                 .DataSource = dsCurveSetup.Tables("CurveSetup")
  18.                 .DisplayMember = "Curve_Name"
  19.                 .ValueMember = "BaseCurve_Setup_ID"
  20.             End With
  21.  
  22. 'SelectedIndexChange procedure code:
  23.  
  24.     Private Sub cmbSingleCurvename_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSingleCurvename.SelectedIndexChanged
  25.  
  26.             ContractNumber = Me.dsCurveSetup.Tables("CurveSetup").Rows(cmbSingleCurvename.SelectedIndex).Item("Contract_ID")
  27.  
  28.             With cmbContract_Name
  29.                 .SelectedValue = ContractNumber
  30.             End With
  31.  
  32. End Sub
  33.  
Mar 26 '08 #1
4 1747
Plater
7,872 Expert 4TB
The selectedindex changed event gets fired when you apply a datasource to those items, so you should make sure the selectedindex != -1 for starters.

As for why the dataset is nothing, it looks like you declare your dataset inside a function so it's no longer in scope for the index changed event?
Mar 26 '08 #2
Hello - thanks for the reply.

The sub which creates the dataset is called from here:

Private Sub fmCurveSetup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TestAdapter()
End Sub

The Test adapter procedure has the following form, so not a function:

Private Sub TestAdapter()
'Code here....
End Sub

I did a watch on the dataset and as soon as it fills the datasource you are right the selectedindexchanged procedure is called for the combobox, but the dataset is nothing within that procedure, whereas when it returns back to the TestAdapter Sub it is not nothing again.

Bizarrely within the Testadapter I have the following code, where the dataset "dsContracts" does persist on selectedIndexChange events for another comboBox which is activated during the same sub (when the datsource is set):

Dim daContracts As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM tbl_Contracts", genCONNECTION)
dsContracts = New DataSet("Contracts")
daContracts.Fill(dsContracts, "Contracts")

Sorry for long reply but I am really stumped, getting the stored procedure to notice that the input parameter had a value to quite a while...
Mar 26 '08 #3
Plater
7,872 Expert 4TB
Well I see this:
Dim dsCurveSetup = New DataSet("CurveSetup")

Which makes me think that, even if you DID declare dsCurveSetup in the spot where it should be, you are creating and populating a new dataset and not the one defined to be used in all functions
Mar 26 '08 #4
Hello - sorry have been an idiot - I have dim'd the dataset twice (once in the sub and once in the class)!!! Thanks for tips anyway!!
Mar 26 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Christian Ahrenkiel | last post: by
2 posts views Thread by Arne | last post: by
1 post views Thread by =?Utf-8?B?SHVzYW0=?= | last post: by
reply views Thread by rickbear | last post: by
4 posts views Thread by Brad | last post: by

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.