Solved

binding wpf combobox to sql server table

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

11 Experts available now in Live!

Get 1:1 Help Now