Solved

binding wpf combobox to sql server table

Posted on 2013-05-10
1
2,590 Views
Last Modified: 2013-05-10
Hi experts,


I saw this example, so I tried doing it:

http://www.codeproject.com/Articles/20439/WPF-DataBinding

I'm using Visual Studio 2010 and Sql Server 2008.
In this example the Categories table from the Northwind database is used.
So I created a WPF application.
The example shown on the link above is using C# as the language but I redid this example using VB instead.

My wpf window containing the ComboBox is called the following:

WindowComboBox2.xaml
WindowComboBox2.xaml.vb

My XAML layout looks like this:

my XAML layout
My Xaml layout markup for WindowComboBox2.xaml is this:

<Window x:Class="WindowComboBox2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WindowComboBox2" Height="303" Width="493" Loaded="OnLoad">
    <Grid>
        <ComboBox Height="18" ItemsSource="{Binding}"  HorizontalAlignment="Right" Margin="0,44,178,0" Name="cmbCategory" VerticalAlignment="Top" Width="176" BorderBrush="#FFFFFFFF" SelectedIndex="0"/>
    </Grid>
</Window>

Open in new window


My Code for WindowComboBox2.xaml.vb looks like this:

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient

Public Class WindowComboBox2

    Private Sub OnLoad(sender As Object, e As System.EventArgs)
        ListCategories()
    End Sub

    Private Sub ListCategories()

        sqlCon = New SqlConnection()
        sqlCon.ConnectionString = "Integrated Security=SSPI;Persist Security Info=False; Initial Catalog=Northwind;Data Source=MySqlServerName"

        cmd = New SqlCommand()
        cmd.Connection = sqlCon
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM Categories"

        sqlDa = New SqlDataAdapter()
        sqlDa.SelectCommand = cmd

        ds = New DataSet()

        Try
            sqlDa.Fill(ds, "Category")

            'Binding the data to the combobox.
            cmbCategory.DataContext = ds.Tables("Category").DefaultView


            'To display category name (DisplayMember in Visual Studio 2005)
            cmbCategory.DisplayMemberPath = ds.Tables("Category").Columns("CategoryName").ToString()

            'To store the ID as hidden (ValueMember in Visual Studio 2005)
            cmbCategory.SelectedValuePath = ds.Tables("Category").Columns("CategoryID").ToString()

        Catch ex As Exception
            MessageBox.Show("An error occurred while loading categories.")
        Finally

            sqlDa.Dispose()
            cmd.Dispose()
            sqlCon.Dispose()

        End Try

    End Sub

End Class

Open in new window


With this code when I look at visual studio this is what i see:

Visual studio code error
and then at the bottom of visual studio under error, i see these 19 errors

errors
Am I missing something? Does someone know what I'm doing wrong?
How do I fix the errors that are showing to get this to work just like the example in the link?
0
Comment
Question by:maqskywalker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39156626
If you hover the mouse cursor over each of the little red 'underline' graphics you should see the errors: those variables are not defined. It should give you the option to create a field of the correct type automatically. If you do that for each error you should see this appear at the start of the class definition:
private SqlConnection sqlCon;
private SqlCommand cmd;
private SqlDataAdapter sqlDa;
private DataSet ds;

Open in new window

With that in place, it should compile ok.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question