By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,763 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,763 IT Pros & Developers. It's quick & easy.

Retriveing a value from a dataset - VB.NET

P: 3

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
  8.             Dim daCurveSetup As OleDbDataAdapter = New OleDbDataAdapter()
  9.             daCurveSetup.SelectCommand = cmd
  11.             Dim dsCurveSetup = New DataSet("CurveSetup")
  13.             daCurveSetup.Fill(dsCurveSetup, "CurveSetup")
  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
  22. 'SelectedIndexChange procedure code:
  24.     Private Sub cmbSingleCurvename_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSingleCurvename.SelectedIndexChanged
  26.             ContractNumber = Me.dsCurveSetup.Tables("CurveSetup").Rows(cmbSingleCurvename.SelectedIndex).Item("Contract_ID")
  28.             With cmbContract_Name
  29.                 .SelectedValue = ContractNumber
  30.             End With
  32. End Sub
Mar 26 '08 #1
Share this Question
Share on Google+
4 Replies

Expert 5K+
P: 7,872
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

P: 3
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
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

Expert 5K+
P: 7,872
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

P: 3
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.