none
Hi.how I can to insert value from sql db to array ? RRS feed

  • Question

  • I have combobox wich biding data from sql db

    I use ado.net

    I wont to insert values wich i get from ado.net code to array ?

    how i can do that?

     

    DataTable dt = new DataTable();

     

    SqlConnection con = new SqlConnection("Data Source=D-PC;Initial Catalog=DMS;Integrated Security=True");

     

    SqlCommand cmd = new SqlCommand("SELECT [CUSTOM_ALIAS] ,[C_DESCRIPT] FROM [MHGROUP].[CUSTOM2]", con);

     

    SqlDataAdapter sqlDa2 = new SqlDataAdapter();

    sqlDa2.SelectCommand = cmd;

    sqlDa2.Fill(dt);

     

    • Moved by Jie Bao Tuesday, September 21, 2010 12:05 PM database and dataset (From:Windows Presentation Foundation (WPF))
    Sunday, September 19, 2010 10:22 AM

Answers

All replies

  • Hi DenisSL1982,

    You can get array of DataRow like this:

    DataRow[] myArray = (from DataRow dr in dt.Rows
                 select dr).ToArray();
    

    Thanks,
    Muhammad

    Sunday, September 19, 2010 4:45 PM
  • Hi Muhammad.

    I biding combobox to array but get this 

    Systsem.Data.DataRow

    Systsem.Data.DataRow

    Systsem.Data.DataRow

    Sunday, September 19, 2010 6:07 PM
  • Hi DenisSL1982,

    You would have to specify the name of column in Path = ColumnName in your Binding section.

    Muhammad

    Sunday, September 19, 2010 6:22 PM
  • I have this working code wich bind data correctly from two string but I need to bind data from sql table how i can change this code to do that?

     

    C # code

     

    public partial class MainWindow : Window, INotifyPropertyChanged

        {

            private ICollectionView _corporation;

            private ICollectionView _corporationWorkspace;

            private ICollectionView _data;

            private object _selectedItem;

     

            public MainWindow()

            {

                InitializeComponent();

     

                Corporation = CollectionViewSource.GetDefaultView(new List<int> { 1018, 1020, 1025 });

                _data = CollectionViewSource.GetDefaultView(new List<string> { "1018/1", "1018/2", "1020/1" });

                Corporation_Workspace = _data;

                this.DataContext = this;

            }

     

            public object SelectedItem

            {

                get { return _selectedItem; }

                set

                {

                    _selectedItem = value;

                    Corporation_Workspace = CollectionViewSource.GetDefaultView(

                        _data.Cast< string >().Where(p => p.StartsWith(_selectedItem.ToString())));

     

                }

            }

     

            public ICollectionView Corporation

            {

                get

                {

                    return _corporation;

                }

                set

                {

                    _corporation = value;

                    OnProertyChanged("Corporation");

                }

            }

     

            public ICollectionView Corporation_Workspace

            {

                get { return _corporationWorkspace; }

                set

                {

                    _corporationWorkspace = value;

                    OnProertyChanged("Corporation_Workspace");

                }

            }

     

            void OnProertyChanged(string propertyName)

            {

                if ( PropertyChanged != null)

                {

                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

                }

            }

     

            public event PropertyChangedEventHandler PropertyChanged;

        }

    }

     

    XAML code

    <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="350" Width="525">

        <StackPanel>

            <ComboBox x:Name="cb1" ItemsSource="{Binding Corporation}" SelectedItem="{Binding SelectedItem}"/>

            <ComboBox x:Name="cb2" ItemsSource="{Binding Corporation_Workspace}"/>

        </StackPanel>

    </Window>

     

    Instead two Yellow strings I need insert two data biding combobox

    from this code.

     

    **********************************************************************************************************

    first Combobox  Dision code

     

    DataSet ds1 = new DataSet();

                    SqlConnection con = new SqlConnection("Data Source=DYS;Initial Catalog=DMS_VEOLIA;Integrated Security=True");

                    SqlCommand cmd1 = new SqlCommand("SELECT CUSTOM_ALIAS, C_DESCRIPT FROM [MHGROUP].CUSTOM3", con);

                    SqlDataAdapter sqlDa1 = new SqlDataAdapter();

                    sqlDa1.SelectCommand = cmd1;

                    sqlDa1.Fill(ds1);

                   

                    DivisonName.ItemsSource = ds1.Tables[0].DefaultView;

                    DivisionCode.ItemsSource = ds1.Tables[0].DefaultView;

                    DivisionCode.SelectedIndex = -1;

    first Combobox  workspace code

     

    DataSet ds1 = new DataSet();

                    SqlConnection con = new SqlConnection("Data Source=DYS-PC;Initial Catalog=DMS_VEOLIA;Integrated Security=True");

                    SqlCommand cmd1 = new SqlCommand("SELECT CUSTOM_ALIAS, C_DESCRIPT FROM [MHGROUP].CUSTOM2", con);

                    SqlDataAdapter sqlDa1 = new SqlDataAdapter();

                    sqlDa1.SelectCommand = cmd1;

                    sqlDa1.Fill(ds1);

     

     

                    WorkspaceCode.ItemsSource = ds1.Tables[0].DefaultView;

                    WorkspaceName.ItemsSource = ds1.Tables[0].DefaultView;

                    WorkspaceName.SelectedIndex = -1;

     

     

     

    Sunday, September 19, 2010 6:54 PM
  • Hi DenisSL1982,

    I move it to ADO.Net DataSet forum, since this question is related with the DataBase and ADO.Net. Hope you can get an answer there.

    Sincerely,
    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Are you looking for a typical code sample? Please download all in one code framework !
    Tuesday, September 21, 2010 12:06 PM
  • ok Thank you.
    Tuesday, September 21, 2010 4:18 PM
  • Hmmm ... I think that this question should have probably remained in the WPF forum ... it's not really about DataSets/DataTables, the question is really more about binding WPF ComboBoxes.

    Could a moderator move it back to the WPF forum?


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Wednesday, September 22, 2010 4:50 AM
  • Maybe this demo could help you:

    WPF CRUD Generator: http://www.codeproject.com/KB/codegen/crudgenerator.aspx

    Hope this could help!

    Wednesday, September 22, 2010 2:30 PM
  • Hi DenisSL1982,

    Is it same with this thread: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/54768b77-b9bc-4546-946c-21a7bcce4d31/

    I have replied, and you could use the SelectedChanged event to query the result (by Linq is easy) form the second ComboBox source, and set the new result to the ComboBox.

    Sincerely,
    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Are you looking for a typical code sample? Please download all in one code framework !
    • Marked as answer by liurong luo Tuesday, September 28, 2010 1:31 AM
    Wednesday, September 22, 2010 3:00 PM