?
Solved

binding wpf combobox to sql server table

Posted on 2013-05-10
1
Medium Priority
?
2,643 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 2000 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 August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

762 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