Solved

binding wpf combobox to sql server table

Posted on 2013-05-10
1
2,527 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now