set selected index on combobox


  • Hi,

    after filling combobox I want to set default value to it because I want to display some data in datagrid depending on combobox value.

    this is my code:

            Dim strSql As New StringBuilder
            strSql.Append("select col1, col2 ")
            strSql.Append(" from tbl ")
            Dim sqlCom As SqlCommand = New SqlCommand(strSql.ToString, sqlConn)
            Dim sqlDs As DataSet = New DataSet
            Dim sqlDa As SqlDataAdapter = New SqlDataAdapter
            sqlDa.SelectCommand = sqlCom
            cmbSkladista.DataContext = sqlDs.Tables(0).DefaultView
            cmbSkladista.DisplayMemberPath = sqlDs.Tables(0).Columns("col1").ToString
            cmbSkladista.SelectedValuePath = sqlDs.Tables(0).Columns("col2").ToString
            cmbSkladista.SelectedIndex = 0
            Dim a As String = cmbSkladista.SelectedValue.ToString

    But after that I get null reference exception. What am I doing wrong?

    Sunday, January 22, 2012 10:19 PM


All replies

  • Have you debugged it? What line do you get the null reference exception on?

    Step through line by line and see what the value for each reference you're using is; one of them is null.

    My educational lectures on C# and .Net in general (ranging from beginner level topics to advanced topics):
    Monday, January 23, 2012 1:26 AM
  • Thanks mario,

    I get an exception on the last line:

    Dim a As String = cmbSkladista.SelectedValue.ToString

    I set selected index on 0 but it is still -1 after debug step on it. And after that when I leave the last line have got an exception.

    This is xaml:

    <StackPanel Orientation="Horizontal" Grid.Row="0">
                <Label Name="lbl<span>Warehouse</span>" Content="<span>Warehouse</span>" Foreground="White"></Label> 
              <ComboBox Name="cmb<span>Warehouse</span>" ItemsSource="{Binding}" Width="170" Height="25" HorizontalAlignment="Left"></ComboBox>
                <Label Name="lblYear" Content="Godina" Foreground="White" Margin="40, 0, 0, 0"></Label>
                <TextBox Name="txtGodina" Width="75" Height="22"></TextBox>


    Monday, January 23, 2012 2:13 PM
  • You have not set the combobox items source (at least not in the code you showed us).  This will result in no items for the combobox and therefore no item to select.


    Hope this helps

    Lloyd Sheen

    Lloyd Sheen
    Monday, January 23, 2012 2:53 PM
  • Yes sqlguy,

    that was solution. I thought that is enough to set DataContext.



    • Proposed as answer by sqlguy Monday, January 23, 2012 8:03 PM
    • Unproposed as answer by sqlguy Monday, January 23, 2012 8:09 PM
    Monday, January 23, 2012 7:50 PM
  • Hi dragy74,

    I think you have resolved your issue, so I close your issue as "Answered".


    best regards,

    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 24, 2012 5:46 AM