locked
How to import a number from SQL server database to WPF form RRS feed

  • Question

  • how can i import a number or value from a SQL server database to my text box in a WPF form using C#
    Monday, November 25, 2013 9:29 AM

Answers

  • First of all you have to clear which technique you will be using to pull data from Sql Database.

    TO my best guess, as you are using WPF, it would be better if you use LINQ to pull data.

    To learn LINQ basics, you can go here.Getting Started with LINQ

    I am dictating the basic child steps i followed to make a example for you.

    1) Create a WPF project in Visual Studio.

    2)Create a SQL Server Database. Right Click in the solution explorer > Add New Item > Service Based Database / Sql Database . * template may vary according to your VS version .mine is 2012.

    3)Create a table in that database with two columns ID and Name.

      Mine datbase is something like this

     

    4) Now add a LINQ to SQL class file from add new items and drag the table in the ORM designer and save the file.

    5)Then i designed my form something with two sections on section contains a Label with a TextBox . There i am going to fetch data from database at forms load event and pass it to the textbox. In the second section i am having a combobox which contains ID's. Whatever ID i select , it's Name is displayed in the second textbox. Here also we are fetching data from SQL.

    here are the codes

    MainWindow.xaml

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="206.537" Width="344.435" Loaded="Window_Loaded_1">
        <Grid>
            <Label Content="Name:" HorizontalAlignment="Left" Margin="31,41,0,0" VerticalAlignment="Top" Height="27" Width="77"/>
            <TextBox HorizontalAlignment="Left" Height="27" Margin="132,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160" Name="txt"/>
            <Separator></Separator>
            <Label Content="Select ProductID:" HorizontalAlignment="Left" Margin="31,104,0,0" VerticalAlignment="Top"/>
            <ComboBox HorizontalAlignment="Left" Margin="154,104,0,0" VerticalAlignment="Top" Width="138" Name="cb" SelectionChanged="cb_SelectionChanged"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="132,143,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160" Name="txtProName"/>
            <Label Content="Product Name:" HorizontalAlignment="Left" Margin="31,143,0,0" VerticalAlignment="Top"/>
    
        </Grid>
    </Window>
    

    MainWindow.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                //addd values to combobox
                cb.Items.Add("1");
                cb.Items.Add("2");
                cb.Items.Add("3");
                cb.Items.Add("4");
            }
    
            private void Window_Loaded_1(object sender, RoutedEventArgs e)
            {
                DataClasses1DataContext db = new DataClasses1DataContext();
                var name = (from n in db.Products
                            where n.Id == 1
                            select n).Single();
                txt.Text = name.Name.ToString();
            }
    
            private void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                int id = Convert.ToInt32(cb.SelectedValue.ToString());
                DataClasses1DataContext db = new DataClasses1DataContext();
                var name = (from n in db.Products
                            where n.Id == id
                            select n).Single();
                txtProName.Text = name.Name.ToString();
    
            }
        }
    }
    

    Hope you understand the code, if any problem just ask.

    Mark the reply as answer and helpful if you find it so. :)


    Vishal Kumar Singh

    Monday, November 25, 2013 1:20 PM

All replies

  • There are several ways to do this. As a beginner I would suggest to use SQLConnection and SQLCommand objects.

    A good read

    http://lamahashim.blogspot.in/2010/04/c-read-insert-update-delete-from-sql.html


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    Monday, November 25, 2013 9:47 AM
  • First of all you have to clear which technique you will be using to pull data from Sql Database.

    TO my best guess, as you are using WPF, it would be better if you use LINQ to pull data.

    To learn LINQ basics, you can go here.Getting Started with LINQ

    I am dictating the basic child steps i followed to make a example for you.

    1) Create a WPF project in Visual Studio.

    2)Create a SQL Server Database. Right Click in the solution explorer > Add New Item > Service Based Database / Sql Database . * template may vary according to your VS version .mine is 2012.

    3)Create a table in that database with two columns ID and Name.

      Mine datbase is something like this

     

    4) Now add a LINQ to SQL class file from add new items and drag the table in the ORM designer and save the file.

    5)Then i designed my form something with two sections on section contains a Label with a TextBox . There i am going to fetch data from database at forms load event and pass it to the textbox. In the second section i am having a combobox which contains ID's. Whatever ID i select , it's Name is displayed in the second textbox. Here also we are fetching data from SQL.

    here are the codes

    MainWindow.xaml

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="206.537" Width="344.435" Loaded="Window_Loaded_1">
        <Grid>
            <Label Content="Name:" HorizontalAlignment="Left" Margin="31,41,0,0" VerticalAlignment="Top" Height="27" Width="77"/>
            <TextBox HorizontalAlignment="Left" Height="27" Margin="132,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160" Name="txt"/>
            <Separator></Separator>
            <Label Content="Select ProductID:" HorizontalAlignment="Left" Margin="31,104,0,0" VerticalAlignment="Top"/>
            <ComboBox HorizontalAlignment="Left" Margin="154,104,0,0" VerticalAlignment="Top" Width="138" Name="cb" SelectionChanged="cb_SelectionChanged"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="132,143,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160" Name="txtProName"/>
            <Label Content="Product Name:" HorizontalAlignment="Left" Margin="31,143,0,0" VerticalAlignment="Top"/>
    
        </Grid>
    </Window>
    

    MainWindow.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                //addd values to combobox
                cb.Items.Add("1");
                cb.Items.Add("2");
                cb.Items.Add("3");
                cb.Items.Add("4");
            }
    
            private void Window_Loaded_1(object sender, RoutedEventArgs e)
            {
                DataClasses1DataContext db = new DataClasses1DataContext();
                var name = (from n in db.Products
                            where n.Id == 1
                            select n).Single();
                txt.Text = name.Name.ToString();
            }
    
            private void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                int id = Convert.ToInt32(cb.SelectedValue.ToString());
                DataClasses1DataContext db = new DataClasses1DataContext();
                var name = (from n in db.Products
                            where n.Id == id
                            select n).Single();
                txtProName.Text = name.Name.ToString();
    
            }
        }
    }
    

    Hope you understand the code, if any problem just ask.

    Mark the reply as answer and helpful if you find it so. :)


    Vishal Kumar Singh

    Monday, November 25, 2013 1:20 PM