data binding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frazer

    #1

    data binding

    hi
    i have a combo box which i populate with all tables from northwind database
    and when the user selects an item from the combo, i want to populate the
    datagrid with data from that table.

    private void FillComboBox()

    {

    DataSet allTablesDataSe t = new DataSet("AllTab les");

    string sqlString;

    this.tableCombo Box.Items.Clear ();

    sqlString = "Select * from sysobjects where type ='u'";

    this.sqlDataAda pter = new SqlDataAdapter( );

    this.sqlDataAda pter.SelectComm and = new SqlCommand(sqlS tring,
    this.sqlConnect ion);

    this.sqlDataAda pter.Fill(allTa blesDataSet,"Al lTables");

    this.tableCombo Box.DataSource = allTablesDataSe t.Tables[0];

    this.tableCombo Box.DisplayMemb er = "name";

    this.tableCombo Box.ValueMember = "name";

    }

    private void OnTableComboBox SelectedIndexCh anged(object sender,
    System.EventArg s e)

    {

    if(this.tableCo mboBox.Selected Index == -1)

    return;

    this.tableDataS et = new DataSet();

    this.sqlDataAda pter.SelectComm and = new SqlCommand("Sel ect * from [" +
    this.tableCombo Box.SelectedVal ue + "]" ,this.sqlConnec tion);

    this.sqlDataAda pter.Fill(this. tableDataSet, "Table");

    this.dataGrid1. DataSource = this.tableDataS et;

    this.dataGrid1. DataMember = "Table";

    }



    however the first time when the program is run i get the foll error. how do
    i resolve that?

    thnx

    An unhandled exception of type 'System.Data.Sq lClient.SqlExce ption' occurred
    in system.data.dll

    Additional information: System error.

    on this line

    this.sqlDataAda pter.Fill(this. tableDataSet, "Table");


  • Cor Ligthert

    #2
    Re: data binding

    Hi Frazer,

    The standard problem with the binded combobox is that when it is loading it
    fires unpredictable the selected index change event.

    Solutions for this are, set a switch in that event function or add the
    handler on the moment that it is completly loaded (when you are reloading it
    than you have to set the switch again or remove the handler at the right
    moment).

    (Why you do that clear of the combobox, if you are reseting it in your
    program, than you should set in my opinion the datasource to null)

    I hope this helps?

    Cor


    Comment

    • William Ryan  eMVP

      #3
      Re: data binding

      Hi Frazier:

      Are you firing another query each time the index changes? you definitely
      don't want to do this. You can just load the data into a datatable and bind
      it to the combobox. Also, you're getting a SQLException which means
      specifically it's a problem w/ the db.

      Cor is exaclty right about the SelectedIndexCh ange and that's ostensibly
      causing this even though it's a db error. Also, don't use Dynamic SQL, use
      Parameters. Nonetheless, you'll want to grab two tables, one for the
      parent, one for the child, and just set the bindings of the grid and the
      CombBox. You'll need to use a DataRelation
      http://www.knowdotnet.com/articles/datarelation.html to make this easy. If
      you do this, you won't have to trap anything on SelectedIndexCh anged to get
      the child records to show up in a grid. This is pretty common and if hit a
      search engine for Master Detail ADO.NET there will be tons of examples.

      Let me know if you need any elaboration.

      HTH,

      Bill

      --
      W.G. Ryan MVP Windows - Embedded




      "Frazer" <Ichor@hotmail. com> wrote in message
      news:Oxy7FNvSEH A.3140@tk2msftn gp13.phx.gbl...[color=blue]
      > hi
      > i have a combo box which i populate with all tables from northwind[/color]
      database[color=blue]
      > and when the user selects an item from the combo, i want to populate the
      > datagrid with data from that table.
      >
      > private void FillComboBox()
      >
      > {
      >
      > DataSet allTablesDataSe t = new DataSet("AllTab les");
      >
      > string sqlString;
      >
      > this.tableCombo Box.Items.Clear ();
      >
      > sqlString = "Select * from sysobjects where type ='u'";
      >
      > this.sqlDataAda pter = new SqlDataAdapter( );
      >
      > this.sqlDataAda pter.SelectComm and = new SqlCommand(sqlS tring,
      > this.sqlConnect ion);
      >
      > this.sqlDataAda pter.Fill(allTa blesDataSet,"Al lTables");
      >
      > this.tableCombo Box.DataSource = allTablesDataSe t.Tables[0];
      >
      > this.tableCombo Box.DisplayMemb er = "name";
      >
      > this.tableCombo Box.ValueMember = "name";
      >
      > }
      >
      > private void OnTableComboBox SelectedIndexCh anged(object sender,
      > System.EventArg s e)
      >
      > {
      >
      > if(this.tableCo mboBox.Selected Index == -1)
      >
      > return;
      >
      > this.tableDataS et = new DataSet();
      >
      > this.sqlDataAda pter.SelectComm and = new SqlCommand("Sel ect * from [" +
      > this.tableCombo Box.SelectedVal ue + "]" ,this.sqlConnec tion);
      >
      > this.sqlDataAda pter.Fill(this. tableDataSet, "Table");
      >
      > this.dataGrid1. DataSource = this.tableDataS et;
      >
      > this.dataGrid1. DataMember = "Table";
      >
      > }
      >
      >
      >
      > however the first time when the program is run i get the foll error. how[/color]
      do[color=blue]
      > i resolve that?
      >
      > thnx
      >
      > An unhandled exception of type 'System.Data.Sq lClient.SqlExce ption'[/color]
      occurred[color=blue]
      > in system.data.dll
      >
      > Additional information: System error.
      >
      > on this line
      >
      > this.sqlDataAda pter.Fill(this. tableDataSet, "Table");
      >
      >[/color]


      Comment

      • Frazer

        #4
        Re: data binding

        What i am doing is populating the combobox with all tables from the
        northwind database and
        when the user selects a particular table i fill the datagrid with rows from
        that table.

        Is my approach too lengthy to achieve this?
        Is master - detail approach the right one to use for this purpose.

        Thanks for your valuable input


        Comment

        • Cor Ligthert

          #5
          Re: data binding

          Hi Frazer,

          I find your approach very nice. Exactly how it should be done in my opinion.

          Cor


          Comment

          • Frazer

            #6
            Re: data binding

            Hi,
            Thanks for your reply,
            However I didnt understand where this would fit in my approach.

            " Nonetheless, you'll want to grab two tables, one for the[color=blue]
            > parent, one for the child, and just set the bindings of the grid and the
            > CombBox. You'll need to use a DataRelation
            > http://www.knowdotnet.com/articles/datarelation.html to make this easy.[/color]
            If[color=blue]
            > you do this, you won't have to trap anything on SelectedIndexCh anged to[/color]
            get[color=blue]
            > the child records to show up in a grid. "[/color]

            Can i bind a datagrid and a combobox ?, so that when i click on certain
            items in the combo the data grid is reflected accordingly???

            Thnx.


            "William Ryan eMVP" <dotnetguru@com cast.nospam.net > wrote in message
            news:uXopm#0SEH A.3672@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Hi Frazier:
            >
            > Are you firing another query each time the index changes? you definitely
            > don't want to do this. You can just load the data into a datatable and[/color]
            bind[color=blue]
            > it to the combobox. Also, you're getting a SQLException which means
            > specifically it's a problem w/ the db.
            >
            > Cor is exaclty right about the SelectedIndexCh ange and that's ostensibly
            > causing this even though it's a db error. Also, don't use Dynamic SQL,[/color]
            use[color=blue]
            > Parameters. Nonetheless, you'll want to grab two tables, one for the
            > parent, one for the child, and just set the bindings of the grid and the
            > CombBox. You'll need to use a DataRelation
            > http://www.knowdotnet.com/articles/datarelation.html to make this easy.[/color]
            If[color=blue]
            > you do this, you won't have to trap anything on SelectedIndexCh anged to[/color]
            get[color=blue]
            > the child records to show up in a grid. This is pretty common and if hit[/color]
            a[color=blue]
            > search engine for Master Detail ADO.NET there will be tons of examples.
            >
            > Let me know if you need any elaboration.
            >
            > HTH,
            >
            > Bill
            >
            > --
            > W.G. Ryan MVP Windows - Embedded
            >
            > http://forums.devbuzz.com
            > http://www.knowdotnet.com/dataaccess.html
            > http://www.msmvps.com/williamryan/
            > "Frazer" <Ichor@hotmail. com> wrote in message
            > news:Oxy7FNvSEH A.3140@tk2msftn gp13.phx.gbl...[color=green]
            > > hi
            > > i have a combo box which i populate with all tables from northwind[/color]
            > database[color=green]
            > > and when the user selects an item from the combo, i want to populate the
            > > datagrid with data from that table.
            > >
            > > private void FillComboBox()
            > >
            > > {
            > >
            > > DataSet allTablesDataSe t = new DataSet("AllTab les");
            > >
            > > string sqlString;
            > >
            > > this.tableCombo Box.Items.Clear ();
            > >
            > > sqlString = "Select * from sysobjects where type ='u'";
            > >
            > > this.sqlDataAda pter = new SqlDataAdapter( );
            > >
            > > this.sqlDataAda pter.SelectComm and = new SqlCommand(sqlS tring,
            > > this.sqlConnect ion);
            > >
            > > this.sqlDataAda pter.Fill(allTa blesDataSet,"Al lTables");
            > >
            > > this.tableCombo Box.DataSource = allTablesDataSe t.Tables[0];
            > >
            > > this.tableCombo Box.DisplayMemb er = "name";
            > >
            > > this.tableCombo Box.ValueMember = "name";
            > >
            > > }
            > >
            > > private void OnTableComboBox SelectedIndexCh anged(object sender,
            > > System.EventArg s e)
            > >
            > > {
            > >
            > > if(this.tableCo mboBox.Selected Index == -1)
            > >
            > > return;
            > >
            > > this.tableDataS et = new DataSet();
            > >
            > > this.sqlDataAda pter.SelectComm and = new SqlCommand("Sel ect * from [" +
            > > this.tableCombo Box.SelectedVal ue + "]" ,this.sqlConnec tion);
            > >
            > > this.sqlDataAda pter.Fill(this. tableDataSet, "Table");
            > >
            > > this.dataGrid1. DataSource = this.tableDataS et;
            > >
            > > this.dataGrid1. DataMember = "Table";
            > >
            > > }
            > >
            > >
            > >
            > > however the first time when the program is run i get the foll error. how[/color]
            > do[color=green]
            > > i resolve that?
            > >
            > > thnx
            > >
            > > An unhandled exception of type 'System.Data.Sq lClient.SqlExce ption'[/color]
            > occurred[color=green]
            > > in system.data.dll
            > >
            > > Additional information: System error.
            > >
            > > on this line
            > >
            > > this.sqlDataAda pter.Fill(this. tableDataSet, "Table");
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...