Solved

binding wpf combobox to sql server table

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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 short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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