Home All Groups Group Topic Archive Search About

How to display multicolumns and get back multi selections form lis

Author
25 Mar 2005 6:43 PM
Alpha
Hi,
I'm working on a Windows applicaton with VS 2003 on windows 2000.  I have a
listbox that I have binded to a dataset table, "source" which has 3 columns. 
I would like to display 2 of those columns, "scode" and "sname", as 1 column
(if not possible then 2 columns will be fine) in the listbox.  Can the
listbox display 2 columns information from the dataset and how can I do that?

Also, I set the property of the listbox to selectionmode to MultiExtend. 
Would this allow the user to select multiple rows form the listbox using the
control key?  If so, then how can I get back the selections from the listbox?

Thanks in advance for any help provided,
Alpha

Author
25 Mar 2005 8:08 PM
Tim Wilson
> I'm working on a Windows applicaton with VS 2003 on windows 2000.  I have
a
> listbox that I have binded to a dataset table, "source" which has 3
columns.
> I would like to display 2 of those columns, "scode" and "sname", as 1
column
> (if not possible then 2 columns will be fine) in the listbox.  Can the
> listbox display 2 columns information from the dataset and how can I do
that?

You could add another column to the DataTable that will combine the data of
the other two columns into one.

private DataTable dataTable1;

....

dataTable1 = new DataTable("MyTable");
dataTable1.Columns.Add("Column1", typeof(string));
dataTable1.Columns.Add("Column2", typeof(string));
dataTable1.Columns.Add("Column3", typeof(string));
dataTable1.Columns.Add("Column1Column2", typeof(string)).Expression =
"Column1 + ', ' + Column2";

for (int x = 0; x < 25; x++)
{
  dataTable1.Rows.Add(new object[] {"FirstName" + x.ToString(), "LastName" +
x.ToString(), "Nothing", null});
}

this.listBox1.DisplayMember = "Column1Column2";
this.listBox1.DataSource = dataTable1;

> Also, I set the property of the listbox to selectionmode to MultiExtend.
> Would this allow the user to select multiple rows form the listbox using
the
> control key?  If so, then how can I get back the selections from the
listbox?

Yes. You can use code similar to the code below to get the selected values.

if (this.listBox1.SelectedItems.Count > 0)
{
  int count = this.listBox1.SelectedItems.Count;
  for (int x = 0; x < count; x++)
  {
    DataRowView rowView = this.listBox1.SelectedItems[x] as DataRowView;
    if (rowView != null)
    {
      MessageBox.Show(rowView.Row["Column1"].ToString());
    }
  }
}

--
Tim Wilson
..Net Compact Framework MVP

Show quote
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:AFB42B7D-8884-4FF1-9BBB-6B165F251610@microsoft.com...
> Hi,
> I'm working on a Windows applicaton with VS 2003 on windows 2000.  I have
a
> listbox that I have binded to a dataset table, "source" which has 3
columns.
> I would like to display 2 of those columns, "scode" and "sname", as 1
column
> (if not possible then 2 columns will be fine) in the listbox.  Can the
> listbox display 2 columns information from the dataset and how can I do
that?
>
> Also, I set the property of the listbox to selectionmode to MultiExtend.
> Would this allow the user to select multiple rows form the listbox using
the
> control key?  If so, then how can I get back the selections from the
listbox?
>
> Thanks in advance for any help provided,
> Alpha
Author
25 Mar 2005 8:25 PM
Alpha
I thought about doing that but wonder if there is a property setting or form
code to do that quick.  I now think that it might be better to use the
datagrid for listing.  I change the listing display/content depending on
which one of the 8 radio buttons that the user selects.  Is it easier to do
multicolumn display and getting the multi-selections back by using the
datagrid control instead.

Thanks,
Alpha

Show quote
"Tim Wilson" wrote:

> > I'm working on a Windows applicaton with VS 2003 on windows 2000.  I have
> a
> > listbox that I have binded to a dataset table, "source" which has 3
> columns.
> > I would like to display 2 of those columns, "scode" and "sname", as 1
> column
> > (if not possible then 2 columns will be fine) in the listbox.  Can the
> > listbox display 2 columns information from the dataset and how can I do
> that?
>
> You could add another column to the DataTable that will combine the data of
> the other two columns into one.
>
> private DataTable dataTable1;
>
> ....
>
> dataTable1 = new DataTable("MyTable");
> dataTable1.Columns.Add("Column1", typeof(string));
> dataTable1.Columns.Add("Column2", typeof(string));
> dataTable1.Columns.Add("Column3", typeof(string));
> dataTable1.Columns.Add("Column1Column2", typeof(string)).Expression =
> "Column1 + ', ' + Column2";
>
> for (int x = 0; x < 25; x++)
> {
>   dataTable1.Rows.Add(new object[] {"FirstName" + x.ToString(), "LastName" +
> x.ToString(), "Nothing", null});
> }
>
> this.listBox1.DisplayMember = "Column1Column2";
> this.listBox1.DataSource = dataTable1;
>
> > Also, I set the property of the listbox to selectionmode to MultiExtend.
> > Would this allow the user to select multiple rows form the listbox using
> the
> > control key?  If so, then how can I get back the selections from the
> listbox?
>
> Yes. You can use code similar to the code below to get the selected values.
>
> if (this.listBox1.SelectedItems.Count > 0)
> {
>   int count = this.listBox1.SelectedItems.Count;
>   for (int x = 0; x < count; x++)
>   {
>     DataRowView rowView = this.listBox1.SelectedItems[x] as DataRowView;
>     if (rowView != null)
>     {
>       MessageBox.Show(rowView.Row["Column1"].ToString());
>     }
>   }
> }
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:AFB42B7D-8884-4FF1-9BBB-6B165F251610@microsoft.com...
> > Hi,
> > I'm working on a Windows applicaton with VS 2003 on windows 2000.  I have
> a
> > listbox that I have binded to a dataset table, "source" which has 3
> columns.
> > I would like to display 2 of those columns, "scode" and "sname", as 1
> column
> > (if not possible then 2 columns will be fine) in the listbox.  Can the
> > listbox display 2 columns information from the dataset and how can I do
> that?
> >
> > Also, I set the property of the listbox to selectionmode to MultiExtend.
> > Would this allow the user to select multiple rows form the listbox using
> the
> > control key?  If so, then how can I get back the selections from the
> listbox?
> >
> > Thanks in advance for any help provided,
> > Alpha
>
>
>
Author
25 Mar 2005 9:11 PM
Tim Wilson
So you're moving to the DataGrid control? That might make more sense if you
need to do multi-column data display from a data source.

--
Tim Wilson
..Net Compact Framework MVP

Show quote
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:73286AF6-2F6E-4842-B0FE-9B66980CC89C@microsoft.com...
> I thought about doing that but wonder if there is a property setting or
form
> code to do that quick.  I now think that it might be better to use the
> datagrid for listing.  I change the listing display/content depending on
> which one of the 8 radio buttons that the user selects.  Is it easier to
do
> multicolumn display and getting the multi-selections back by using the
> datagrid control instead.
>
> Thanks,
> Alpha
>
> "Tim Wilson" wrote:
>
> > > I'm working on a Windows applicaton with VS 2003 on windows 2000.  I
have
> > a
> > > listbox that I have binded to a dataset table, "source" which has 3
> > columns.
> > > I would like to display 2 of those columns, "scode" and "sname", as 1
> > column
> > > (if not possible then 2 columns will be fine) in the listbox.  Can the
> > > listbox display 2 columns information from the dataset and how can I
do
> > that?
> >
> > You could add another column to the DataTable that will combine the data
of
> > the other two columns into one.
> >
> > private DataTable dataTable1;
> >
> > ....
> >
> > dataTable1 = new DataTable("MyTable");
> > dataTable1.Columns.Add("Column1", typeof(string));
> > dataTable1.Columns.Add("Column2", typeof(string));
> > dataTable1.Columns.Add("Column3", typeof(string));
> > dataTable1.Columns.Add("Column1Column2", typeof(string)).Expression =
> > "Column1 + ', ' + Column2";
> >
> > for (int x = 0; x < 25; x++)
> > {
> >   dataTable1.Rows.Add(new object[] {"FirstName" + x.ToString(),
"LastName" +
> > x.ToString(), "Nothing", null});
> > }
> >
> > this.listBox1.DisplayMember = "Column1Column2";
> > this.listBox1.DataSource = dataTable1;
> >
> > > Also, I set the property of the listbox to selectionmode to
MultiExtend.
> > > Would this allow the user to select multiple rows form the listbox
using
> > the
> > > control key?  If so, then how can I get back the selections from the
> > listbox?
> >
> > Yes. You can use code similar to the code below to get the selected
values.
> >
> > if (this.listBox1.SelectedItems.Count > 0)
> > {
> >   int count = this.listBox1.SelectedItems.Count;
> >   for (int x = 0; x < count; x++)
> >   {
> >     DataRowView rowView = this.listBox1.SelectedItems[x] as DataRowView;
> >     if (rowView != null)
> >     {
> >       MessageBox.Show(rowView.Row["Column1"].ToString());
> >     }
> >   }
> > }
> >
> > --
> > Tim Wilson
> > ..Net Compact Framework MVP
> >
> > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > news:AFB42B7D-8884-4FF1-9BBB-6B165F251610@microsoft.com...
> > > Hi,
> > > I'm working on a Windows applicaton with VS 2003 on windows 2000.  I
have
> > a
> > > listbox that I have binded to a dataset table, "source" which has 3
> > columns.
> > > I would like to display 2 of those columns, "scode" and "sname", as 1
> > column
> > > (if not possible then 2 columns will be fine) in the listbox.  Can the
> > > listbox display 2 columns information from the dataset and how can I
do
> > that?
> > >
> > > Also, I set the property of the listbox to selectionmode to
MultiExtend.
> > > Would this allow the user to select multiple rows form the listbox
using
> > the
> > > control key?  If so, then how can I get back the selections from the
> > listbox?
> > >
> > > Thanks in advance for any help provided,
> > > Alpha
> >
> >
> >
Author
25 Mar 2005 9:23 PM
Alpha
Yes, I'm working on it now.  Thank you very much for your help and have a
great weekend.

Alpha

Show quote
"Tim Wilson" wrote:

> So you're moving to the DataGrid control? That might make more sense if you
> need to do multi-column data display from a data source.
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:73286AF6-2F6E-4842-B0FE-9B66980CC89C@microsoft.com...
> > I thought about doing that but wonder if there is a property setting or
> form
> > code to do that quick.  I now think that it might be better to use the
> > datagrid for listing.  I change the listing display/content depending on
> > which one of the 8 radio buttons that the user selects.  Is it easier to
> do
> > multicolumn display and getting the multi-selections back by using the
> > datagrid control instead.
> >
> > Thanks,
> > Alpha
> >
> > "Tim Wilson" wrote:
> >
> > > > I'm working on a Windows applicaton with VS 2003 on windows 2000.  I
> have
> > > a
> > > > listbox that I have binded to a dataset table, "source" which has 3
> > > columns.
> > > > I would like to display 2 of those columns, "scode" and "sname", as 1
> > > column
> > > > (if not possible then 2 columns will be fine) in the listbox.  Can the
> > > > listbox display 2 columns information from the dataset and how can I
> do
> > > that?
> > >
> > > You could add another column to the DataTable that will combine the data
> of
> > > the other two columns into one.
> > >
> > > private DataTable dataTable1;
> > >
> > > ....
> > >
> > > dataTable1 = new DataTable("MyTable");
> > > dataTable1.Columns.Add("Column1", typeof(string));
> > > dataTable1.Columns.Add("Column2", typeof(string));
> > > dataTable1.Columns.Add("Column3", typeof(string));
> > > dataTable1.Columns.Add("Column1Column2", typeof(string)).Expression =
> > > "Column1 + ', ' + Column2";
> > >
> > > for (int x = 0; x < 25; x++)
> > > {
> > >   dataTable1.Rows.Add(new object[] {"FirstName" + x.ToString(),
> "LastName" +
> > > x.ToString(), "Nothing", null});
> > > }
> > >
> > > this.listBox1.DisplayMember = "Column1Column2";
> > > this.listBox1.DataSource = dataTable1;
> > >
> > > > Also, I set the property of the listbox to selectionmode to
> MultiExtend.
> > > > Would this allow the user to select multiple rows form the listbox
> using
> > > the
> > > > control key?  If so, then how can I get back the selections from the
> > > listbox?
> > >
> > > Yes. You can use code similar to the code below to get the selected
> values.
> > >
> > > if (this.listBox1.SelectedItems.Count > 0)
> > > {
> > >   int count = this.listBox1.SelectedItems.Count;
> > >   for (int x = 0; x < count; x++)
> > >   {
> > >     DataRowView rowView = this.listBox1.SelectedItems[x] as DataRowView;
> > >     if (rowView != null)
> > >     {
> > >       MessageBox.Show(rowView.Row["Column1"].ToString());
> > >     }
> > >   }
> > > }
> > >
> > > --
> > > Tim Wilson
> > > ..Net Compact Framework MVP
> > >
> > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > news:AFB42B7D-8884-4FF1-9BBB-6B165F251610@microsoft.com...
> > > > Hi,
> > > > I'm working on a Windows applicaton with VS 2003 on windows 2000.  I
> have
> > > a
> > > > listbox that I have binded to a dataset table, "source" which has 3
> > > columns.
> > > > I would like to display 2 of those columns, "scode" and "sname", as 1
> > > column
> > > > (if not possible then 2 columns will be fine) in the listbox.  Can the
> > > > listbox display 2 columns information from the dataset and how can I
> do
> > > that?
> > > >
> > > > Also, I set the property of the listbox to selectionmode to
> MultiExtend.
> > > > Would this allow the user to select multiple rows form the listbox
> using
> > > the
> > > > control key?  If so, then how can I get back the selections from the
> > > listbox?
> > > >
> > > > Thanks in advance for any help provided,
> > > > Alpha
> > >
> > >
> > >
>
>
>
Author
31 Mar 2005 7:47 PM
Alpha
I'm back to using the listbox with a newly added column to display the 2
cloumns data from the dataset.  Question, how can I retrieve the valuemembers
from the selected items of the list box?  Many thanks in adavnce, Alpha

            try
            {
                if (radioAttendant.Checked == true)
                {
                    //Setup the PaySource in listbox
                    cmdLocal.CommandText = "select eid, ename from tblEmployee" ;
                    saLocal.Fill(dsLocal,"attendant");

                    drv = (DataRowView) BindingContext[dsLocal.Tables["attendant"]].Current;
                    dvAttendant = new
DataView(dsLocal.Tables["attendant"],"","ename",DataViewRowState.CurrentRows);
                    this.lstSelections.DataSource = dvAttendant;
                    lstSelections.DisplayMember = "ename";
                    lstSelections.ValueMember = "eid";   
                }
                else
                {
                    dsLocal.Tables["attendant"].Clear();
                    dsLocal.Tables["attendant"].Dispose();
                    dvAttendant.Dispose();

                }   
            }
            catch(Exception my_e)
            {
                MessageBox.Show("The system encountered an error loading listing
data:\n" + my_e.ToString());
            }

Show quote
"Tim Wilson" wrote:

> So you're moving to the DataGrid control? That might make more sense if you
> need to do multi-column data display from a data source.
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:73286AF6-2F6E-4842-B0FE-9B66980CC89C@microsoft.com...
> > I thought about doing that but wonder if there is a property setting or
> form
> > code to do that quick.  I now think that it might be better to use the
> > datagrid for listing.  I change the listing display/content depending on
> > which one of the 8 radio buttons that the user selects.  Is it easier to
> do
> > multicolumn display and getting the multi-selections back by using the
> > datagrid control instead.
> >
> > Thanks,
> > Alpha
> >
> > "Tim Wilson" wrote:
> >
> > > > I'm working on a Windows applicaton with VS 2003 on windows 2000.  I
> have
> > > a
> > > > listbox that I have binded to a dataset table, "source" which has 3
> > > columns.
> > > > I would like to display 2 of those columns, "scode" and "sname", as 1
> > > column
> > > > (if not possible then 2 columns will be fine) in the listbox.  Can the
> > > > listbox display 2 columns information from the dataset and how can I
> do
> > > that?
> > >
> > > You could add another column to the DataTable that will combine the data
> of
> > > the other two columns into one.
> > >
> > > private DataTable dataTable1;
> > >
> > > ....
> > >
> > > dataTable1 = new DataTable("MyTable");
> > > dataTable1.Columns.Add("Column1", typeof(string));
> > > dataTable1.Columns.Add("Column2", typeof(string));
> > > dataTable1.Columns.Add("Column3", typeof(string));
> > > dataTable1.Columns.Add("Column1Column2", typeof(string)).Expression =
> > > "Column1 + ', ' + Column2";
> > >
> > > for (int x = 0; x < 25; x++)
> > > {
> > >   dataTable1.Rows.Add(new object[] {"FirstName" + x.ToString(),
> "LastName" +
> > > x.ToString(), "Nothing", null});
> > > }
> > >
> > > this.listBox1.DisplayMember = "Column1Column2";
> > > this.listBox1.DataSource = dataTable1;
> > >
> > > > Also, I set the property of the listbox to selectionmode to
> MultiExtend.
> > > > Would this allow the user to select multiple rows form the listbox
> using
> > > the
> > > > control key?  If so, then how can I get back the selections from the
> > > listbox?
> > >
> > > Yes. You can use code similar to the code below to get the selected
> values.
> > >
> > > if (this.listBox1.SelectedItems.Count > 0)
> > > {
> > >   int count = this.listBox1.SelectedItems.Count;
> > >   for (int x = 0; x < count; x++)
> > >   {
> > >     DataRowView rowView = this.listBox1.SelectedItems[x] as DataRowView;
> > >     if (rowView != null)
> > >     {
> > >       MessageBox.Show(rowView.Row["Column1"].ToString());
> > >     }
> > >   }
> > > }
> > >
> > > --
> > > Tim Wilson
> > > ..Net Compact Framework MVP
> > >
> > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > news:AFB42B7D-8884-4FF1-9BBB-6B165F251610@microsoft.com...
> > > > Hi,
> > > > I'm working on a Windows applicaton with VS 2003 on windows 2000.  I
> have
> > > a
> > > > listbox that I have binded to a dataset table, "source" which has 3
> > > columns.
> > > > I would like to display 2 of those columns, "scode" and "sname", as 1
> > > column
> > > > (if not possible then 2 columns will be fine) in the listbox.  Can the
> > > > listbox display 2 columns information from the dataset and how can I
> do
> > > that?
> > > >
> > > > Also, I set the property of the listbox to selectionmode to
> MultiExtend.
> > > > Would this allow the user to select multiple rows form the listbox
> using
> > > the
> > > > control key?  If so, then how can I get back the selections from the
> > > listbox?
> > > >
> > > > Thanks in advance for any help provided,
> > > > Alpha
> > >
> > >
> > >
>
>
>
Author
31 Mar 2005 8:15 PM
Tim Wilson
Something similar to the code below should work.

if (this.listBox1.SelectedItems.Count > 0)
{
  int count = this.listBox1.SelectedItems.Count;
  for (int x = 0; x < count; x++)
  {
    DataRowView rowView = this.listBox1.SelectedItems[x] as DataRowView;
    if (rowView != null)
    {
      MessageBox.Show(rowView.Row[this.listBox1.ValueMember]ToString());
    }
  }
}

--
Tim Wilson
..Net Compact Framework MVP

Show quote
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:54CC228B-063D-434A-8151-F7063191F61C@microsoft.com...
> I'm back to using the listbox with a newly added column to display the 2
> cloumns data from the dataset.  Question, how can I retrieve the
valuemembers
>  from the selected items of the list box?  Many thanks in adavnce, Alpha
>
> try
> {
> if (radioAttendant.Checked == true)
> {
> //Setup the PaySource in listbox
> cmdLocal.CommandText = "select eid, ename from tblEmployee" ;
> saLocal.Fill(dsLocal,"attendant");
>
> drv = (DataRowView) BindingContext[dsLocal.Tables["attendant"]].Current;
> dvAttendant = new
>
DataView(dsLocal.Tables["attendant"],"","ename",DataViewRowState.CurrentRows
);
Show quote
> this.lstSelections.DataSource = dvAttendant;
> lstSelections.DisplayMember = "ename";
> lstSelections.ValueMember = "eid";
> }
> else
> {
> dsLocal.Tables["attendant"].Clear();
> dsLocal.Tables["attendant"].Dispose();
> dvAttendant.Dispose();
>
> }
> }
> catch(Exception my_e)
> {
> MessageBox.Show("The system encountered an error loading listing
> data:\n" + my_e.ToString());
> }
>
> "Tim Wilson" wrote:
>
> > So you're moving to the DataGrid control? That might make more sense if
you
> > need to do multi-column data display from a data source.
> >
> > --
> > Tim Wilson
> > ..Net Compact Framework MVP
> >
> > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > news:73286AF6-2F6E-4842-B0FE-9B66980CC89C@microsoft.com...
> > > I thought about doing that but wonder if there is a property setting
or
> > form
> > > code to do that quick.  I now think that it might be better to use the
> > > datagrid for listing.  I change the listing display/content depending
on
> > > which one of the 8 radio buttons that the user selects.  Is it easier
to
> > do
> > > multicolumn display and getting the multi-selections back by using the
> > > datagrid control instead.
> > >
> > > Thanks,
> > > Alpha
> > >
> > > "Tim Wilson" wrote:
> > >
> > > > > I'm working on a Windows applicaton with VS 2003 on windows 2000.
I
> > have
> > > > a
> > > > > listbox that I have binded to a dataset table, "source" which has
3
> > > > columns.
> > > > > I would like to display 2 of those columns, "scode" and "sname",
as 1
> > > > column
> > > > > (if not possible then 2 columns will be fine) in the listbox.  Can
the
> > > > > listbox display 2 columns information from the dataset and how can
I
> > do
> > > > that?
> > > >
> > > > You could add another column to the DataTable that will combine the
data
> > of
> > > > the other two columns into one.
> > > >
> > > > private DataTable dataTable1;
> > > >
> > > > ....
> > > >
> > > > dataTable1 = new DataTable("MyTable");
> > > > dataTable1.Columns.Add("Column1", typeof(string));
> > > > dataTable1.Columns.Add("Column2", typeof(string));
> > > > dataTable1.Columns.Add("Column3", typeof(string));
> > > > dataTable1.Columns.Add("Column1Column2", typeof(string)).Expression
=
> > > > "Column1 + ', ' + Column2";
> > > >
> > > > for (int x = 0; x < 25; x++)
> > > > {
> > > >   dataTable1.Rows.Add(new object[] {"FirstName" + x.ToString(),
> > "LastName" +
> > > > x.ToString(), "Nothing", null});
> > > > }
> > > >
> > > > this.listBox1.DisplayMember = "Column1Column2";
> > > > this.listBox1.DataSource = dataTable1;
> > > >
> > > > > Also, I set the property of the listbox to selectionmode to
> > MultiExtend.
> > > > > Would this allow the user to select multiple rows form the listbox
> > using
> > > > the
> > > > > control key?  If so, then how can I get back the selections from
the
> > > > listbox?
> > > >
> > > > Yes. You can use code similar to the code below to get the selected
> > values.
> > > >
> > > > if (this.listBox1.SelectedItems.Count > 0)
> > > > {
> > > >   int count = this.listBox1.SelectedItems.Count;
> > > >   for (int x = 0; x < count; x++)
> > > >   {
> > > >     DataRowView rowView = this.listBox1.SelectedItems[x] as
DataRowView;
> > > >     if (rowView != null)
> > > >     {
> > > >       MessageBox.Show(rowView.Row["Column1"].ToString());
> > > >     }
> > > >   }
> > > > }
> > > >
> > > > --
> > > > Tim Wilson
> > > > ..Net Compact Framework MVP
> > > >
> > > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > > news:AFB42B7D-8884-4FF1-9BBB-6B165F251610@microsoft.com...
> > > > > Hi,
> > > > > I'm working on a Windows applicaton with VS 2003 on windows 2000.
I
> > have
> > > > a
> > > > > listbox that I have binded to a dataset table, "source" which has
3
> > > > columns.
> > > > > I would like to display 2 of those columns, "scode" and "sname",
as 1
> > > > column
> > > > > (if not possible then 2 columns will be fine) in the listbox.  Can
the
> > > > > listbox display 2 columns information from the dataset and how can
I
> > do
> > > > that?
> > > > >
> > > > > Also, I set the property of the listbox to selectionmode to
> > MultiExtend.
> > > > > Would this allow the user to select multiple rows form the listbox
> > using
> > > > the
> > > > > control key?  If so, then how can I get back the selections from
the
> > > > listbox?
> > > > >
> > > > > Thanks in advance for any help provided,
> > > > > Alpha
> > > >
> > > >
> > > >
> >
> >
> >
Author
31 Mar 2005 8:41 PM
Alpha
I dispose the table that contain the new column that I added (to display 2
columns of data on the listbox) when I de-select the radio button.  I get an
error about ducplicat column existed alreday when I re-select the radio
button because my code would create the table and the new column.  Why isn't
the added column removed when I dispose the table?  How do I get rid of it? 
Thanks for your help, Alpha

        private void radioPayPlan_CheckedChanged(object sender, System.EventArgs e)
        {
            try
            {
                if (radioPayPlan.Checked == true)
                {
                    //Setup the PaySource in listbox
                    cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
                    saLocal.Fill(dsLocal,"source");

                    drv = (DataRowView) BindingContext[dsLocal.Tables["source"]].Current;
                    dsLocal.Tables["source"].AcceptChanges();
                    //Create new column
                    DataColumn sc = new
DataColumn("ScodeName",Type.GetType("System.String"));
                    sc.Expression = "iif(len(scode) > 0,scode + ': ' + sname,sname)";
                    dsLocal.Tables["source"].Columns.Add(sc);
                    //this.lstSelections.SelectedIndex = 1;

                    dvSource = new
DataView(dsLocal.Tables["source"],"","sname",DataViewRowState.CurrentRows);
                    this.lstSelections.DataSource = dvSource;
                    lstSelections.DisplayMember = "ScodeName";
                    lstSelections.ValueMember = "sID";   
                }
                else
                {
                    dsLocal.Tables["source"].Clear();
                    dsLocal.Tables["source"].Dispose();
                    dvSource.Dispose();

                }
            }
            catch(Exception my_e)
            {
                MessageBox.Show("The system encountered an error loading listing
data:\n" + my_e.ToString());
            }

        }


Show quote
"Tim Wilson" wrote:

> Something similar to the code below should work.
>
> if (this.listBox1.SelectedItems.Count > 0)
> {
>   int count = this.listBox1.SelectedItems.Count;
>   for (int x = 0; x < count; x++)
>   {
>     DataRowView rowView = this.listBox1.SelectedItems[x] as DataRowView;
>     if (rowView != null)
>     {
>       MessageBox.Show(rowView.Row[this.listBox1.ValueMember]ToString());
>     }
>   }
> }
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:54CC228B-063D-434A-8151-F7063191F61C@microsoft.com...
> > I'm back to using the listbox with a newly added column to display the 2
> > cloumns data from the dataset.  Question, how can I retrieve the
> valuemembers
> >  from the selected items of the list box?  Many thanks in adavnce, Alpha
> >
> > try
> > {
> > if (radioAttendant.Checked == true)
> > {
> > //Setup the PaySource in listbox
> > cmdLocal.CommandText = "select eid, ename from tblEmployee" ;
> > saLocal.Fill(dsLocal,"attendant");
> >
> > drv = (DataRowView) BindingContext[dsLocal.Tables["attendant"]].Current;
> > dvAttendant = new
> >
> DataView(dsLocal.Tables["attendant"],"","ename",DataViewRowState.CurrentRows
> );
> > this.lstSelections.DataSource = dvAttendant;
> > lstSelections.DisplayMember = "ename";
> > lstSelections.ValueMember = "eid";
> > }
> > else
> > {
> > dsLocal.Tables["attendant"].Clear();
> > dsLocal.Tables["attendant"].Dispose();
> > dvAttendant.Dispose();
> >
> > }
> > }
> > catch(Exception my_e)
> > {
> > MessageBox.Show("The system encountered an error loading listing
> > data:\n" + my_e.ToString());
> > }
> >
> > "Tim Wilson" wrote:
> >
> > > So you're moving to the DataGrid control? That might make more sense if
> you
> > > need to do multi-column data display from a data source.
> > >
> > > --
> > > Tim Wilson
> > > ..Net Compact Framework MVP
> > >
> > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > news:73286AF6-2F6E-4842-B0FE-9B66980CC89C@microsoft.com...
> > > > I thought about doing that but wonder if there is a property setting
> or
> > > form
> > > > code to do that quick.  I now think that it might be better to use the
> > > > datagrid for listing.  I change the listing display/content depending
> on
> > > > which one of the 8 radio buttons that the user selects.  Is it easier
> to
> > > do
> > > > multicolumn display and getting the multi-selections back by using the
> > > > datagrid control instead.
> > > >
> > > > Thanks,
> > > > Alpha
> > > >
> > > > "Tim Wilson" wrote:
> > > >
> > > > > > I'm working on a Windows applicaton with VS 2003 on windows 2000.
> I
> > > have
> > > > > a
> > > > > > listbox that I have binded to a dataset table, "source" which has
> 3
> > > > > columns.
> > > > > > I would like to display 2 of those columns, "scode" and "sname",
> as 1
> > > > > column
> > > > > > (if not possible then 2 columns will be fine) in the listbox.  Can
> the
> > > > > > listbox display 2 columns information from the dataset and how can
> I
> > > do
> > > > > that?
> > > > >
> > > > > You could add another column to the DataTable that will combine the
> data
> > > of
> > > > > the other two columns into one.
> > > > >
> > > > > private DataTable dataTable1;
> > > > >
> > > > > ....
> > > > >
> > > > > dataTable1 = new DataTable("MyTable");
> > > > > dataTable1.Columns.Add("Column1", typeof(string));
> > > > > dataTable1.Columns.Add("Column2", typeof(string));
> > > > > dataTable1.Columns.Add("Column3", typeof(string));
> > > > > dataTable1.Columns.Add("Column1Column2", typeof(string)).Expression
> =
> > > > > "Column1 + ', ' + Column2";
> > > > >
> > > > > for (int x = 0; x < 25; x++)
> > > > > {
> > > > >   dataTable1.Rows.Add(new object[] {"FirstName" + x.ToString(),
> > > "LastName" +
> > > > > x.ToString(), "Nothing", null});
> > > > > }
> > > > >
> > > > > this.listBox1.DisplayMember = "Column1Column2";
> > > > > this.listBox1.DataSource = dataTable1;
> > > > >
> > > > > > Also, I set the property of the listbox to selectionmode to
> > > MultiExtend.
> > > > > > Would this allow the user to select multiple rows form the listbox
> > > using
> > > > > the
> > > > > > control key?  If so, then how can I get back the selections from
> the
> > > > > listbox?
> > > > >
> > > > > Yes. You can use code similar to the code below to get the selected
> > > values.
> > > > >
> > > > > if (this.listBox1.SelectedItems.Count > 0)
> > > > > {
> > > > >   int count = this.listBox1.SelectedItems.Count;
> > > > >   for (int x = 0; x < count; x++)
> > > > >   {
> > > > >     DataRowView rowView = this.listBox1.SelectedItems[x] as
> DataRowView;
> > > > >     if (rowView != null)
> > > > >     {
> > > > >       MessageBox.Show(rowView.Row["Column1"].ToString());
> > > > >     }
> > > > >   }
> > > > > }
> > > > >
> > > > > --
> > > > > Tim Wilson
> > > > > ..Net Compact Framework MVP
> > > > >
> > > > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > > > news:AFB42B7D-8884-4FF1-9BBB-6B165F251610@microsoft.com...
> > > > > > Hi,
> > > > > > I'm working on a Windows applicaton with VS 2003 on windows 2000.
> I
> > > have
> > > > > a
> > > > > > listbox that I have binded to a dataset table, "source" which has
> 3
> > > > > columns.
> > > > > > I would like to display 2 of those columns, "scode" and "sname",
> as 1
> > > > > column
> > > > > > (if not possible then 2 columns will be fine) in the listbox.  Can
> the
> > > > > > listbox display 2 columns information from the dataset and how can
> I
> > > do
> > > > > that?
> > > > > >
> > > > > > Also, I set the property of the listbox to selectionmode to
> > > MultiExtend.
> > > > > > Would this allow the user to select multiple rows form the listbox
> > > using
> > > > > the
> > > > > > control key?  If so, then how can I get back the selections from
> the
> > > > > listbox?
> > > > > >
> > > > > > Thanks in advance for any help provided,
> > > > > > Alpha
> > > > >
> > > > >
> > > > >
> > >
> > >
> > >
>
>
>
Author
31 Mar 2005 10:08 PM
Tim Wilson
I don't see, in the code provided, where you are creating the DataTable.
Once you've disposed of the DataTable you should create a new instance of
it. The new instance can't possibly know about the column that you added to
the previous instance - they are two separate objects. I would say you have
two options.
(1) Simply clear the DataTable of all it's data and leave the empty
DataTable in the DataSet. Do not dispose of it.
(2) Remove the DataTable from the DataSet, dispose of it, and then you'll
need to recreate it, add the new column, and add it back to the DataSet when
appropriate.

--
Tim Wilson
..Net Compact Framework MVP

Show quote
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:6866F615-D1A1-45EF-ADA4-DED2A196BDFD@microsoft.com...
> I dispose the table that contain the new column that I added (to display 2
> columns of data on the listbox) when I de-select the radio button.  I get
an
> error about ducplicat column existed alreday when I re-select the radio
> button because my code would create the table and the new column.  Why
isn't
> the added column removed when I dispose the table?  How do I get rid of
it?
> Thanks for your help, Alpha
>
> private void radioPayPlan_CheckedChanged(object sender, System.EventArgs
e)
> {
> try
> {
> if (radioPayPlan.Checked == true)
> {
> //Setup the PaySource in listbox
> cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
> saLocal.Fill(dsLocal,"source");
>
> drv = (DataRowView) BindingContext[dsLocal.Tables["source"]].Current;
> dsLocal.Tables["source"].AcceptChanges();
> //Create new column
> DataColumn sc = new
> DataColumn("ScodeName",Type.GetType("System.String"));
> sc.Expression = "iif(len(scode) > 0,scode + ': ' + sname,sname)";
> dsLocal.Tables["source"].Columns.Add(sc);
> //this.lstSelections.SelectedIndex = 1;
>
> dvSource = new
>
DataView(dsLocal.Tables["source"],"","sname",DataViewRowState.CurrentRows);
Show quote
> this.lstSelections.DataSource = dvSource;
> lstSelections.DisplayMember = "ScodeName";
> lstSelections.ValueMember = "sID";
> }
> else
> {
> dsLocal.Tables["source"].Clear();
> dsLocal.Tables["source"].Dispose();
> dvSource.Dispose();
>
> }
> }
> catch(Exception my_e)
> {
> MessageBox.Show("The system encountered an error loading listing
> data:\n" + my_e.ToString());
> }
>
> }
>
>
> "Tim Wilson" wrote:
>
> > Something similar to the code below should work.
> >
> > if (this.listBox1.SelectedItems.Count > 0)
> > {
> >   int count = this.listBox1.SelectedItems.Count;
> >   for (int x = 0; x < count; x++)
> >   {
> >     DataRowView rowView = this.listBox1.SelectedItems[x] as DataRowView;
> >     if (rowView != null)
> >     {
> >       MessageBox.Show(rowView.Row[this.listBox1.ValueMember]ToString());
> >     }
> >   }
> > }
> >
> > --
> > Tim Wilson
> > ..Net Compact Framework MVP
> >
> > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > news:54CC228B-063D-434A-8151-F7063191F61C@microsoft.com...
> > > I'm back to using the listbox with a newly added column to display the
2
> > > cloumns data from the dataset.  Question, how can I retrieve the
> > valuemembers
> > >  from the selected items of the list box?  Many thanks in adavnce,
Alpha
> > >
> > > try
> > > {
> > > if (radioAttendant.Checked == true)
> > > {
> > > //Setup the PaySource in listbox
> > > cmdLocal.CommandText = "select eid, ename from tblEmployee" ;
> > > saLocal.Fill(dsLocal,"attendant");
> > >
> > > drv = (DataRowView)
BindingContext[dsLocal.Tables["attendant"]].Current;
> > > dvAttendant = new
> > >
> >
DataView(dsLocal.Tables["attendant"],"","ename",DataViewRowState.CurrentRows
Show quote
> > );
> > > this.lstSelections.DataSource = dvAttendant;
> > > lstSelections.DisplayMember = "ename";
> > > lstSelections.ValueMember = "eid";
> > > }
> > > else
> > > {
> > > dsLocal.Tables["attendant"].Clear();
> > > dsLocal.Tables["attendant"].Dispose();
> > > dvAttendant.Dispose();
> > >
> > > }
> > > }
> > > catch(Exception my_e)
> > > {
> > > MessageBox.Show("The system encountered an error loading listing
> > > data:\n" + my_e.ToString());
> > > }
> > >
> > > "Tim Wilson" wrote:
> > >
> > > > So you're moving to the DataGrid control? That might make more sense
if
> > you
> > > > need to do multi-column data display from a data source.
> > > >
> > > > --
> > > > Tim Wilson
> > > > ..Net Compact Framework MVP
> > > >
> > > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > > news:73286AF6-2F6E-4842-B0FE-9B66980CC89C@microsoft.com...
> > > > > I thought about doing that but wonder if there is a property
setting
> > or
> > > > form
> > > > > code to do that quick.  I now think that it might be better to use
the
> > > > > datagrid for listing.  I change the listing display/content
depending
> > on
> > > > > which one of the 8 radio buttons that the user selects.  Is it
easier
> > to
> > > > do
> > > > > multicolumn display and getting the multi-selections back by using
the
> > > > > datagrid control instead.
> > > > >
> > > > > Thanks,
> > > > > Alpha
> > > > >
> > > > > "Tim Wilson" wrote:
> > > > >
> > > > > > > I'm working on a Windows applicaton with VS 2003 on windows
2000.
> > I
> > > > have
> > > > > > a
> > > > > > > listbox that I have binded to a dataset table, "source" which
has
> > 3
> > > > > > columns.
> > > > > > > I would like to display 2 of those columns, "scode" and
"sname",
> > as 1
> > > > > > column
> > > > > > > (if not possible then 2 columns will be fine) in the listbox.
Can
> > the
> > > > > > > listbox display 2 columns information from the dataset and how
can
> > I
> > > > do
> > > > > > that?
> > > > > >
> > > > > > You could add another column to the DataTable that will combine
the
> > data
> > > > of
> > > > > > the other two columns into one.
> > > > > >
> > > > > > private DataTable dataTable1;
> > > > > >
> > > > > > ....
> > > > > >
> > > > > > dataTable1 = new DataTable("MyTable");
> > > > > > dataTable1.Columns.Add("Column1", typeof(string));
> > > > > > dataTable1.Columns.Add("Column2", typeof(string));
> > > > > > dataTable1.Columns.Add("Column3", typeof(string));
> > > > > > dataTable1.Columns.Add("Column1Column2",
typeof(string)).Expression
Show quote
> > =
> > > > > > "Column1 + ', ' + Column2";
> > > > > >
> > > > > > for (int x = 0; x < 25; x++)
> > > > > > {
> > > > > >   dataTable1.Rows.Add(new object[] {"FirstName" + x.ToString(),
> > > > "LastName" +
> > > > > > x.ToString(), "Nothing", null});
> > > > > > }
> > > > > >
> > > > > > this.listBox1.DisplayMember = "Column1Column2";
> > > > > > this.listBox1.DataSource = dataTable1;
> > > > > >
> > > > > > > Also, I set the property of the listbox to selectionmode to
> > > > MultiExtend.
> > > > > > > Would this allow the user to select multiple rows form the
listbox
> > > > using
> > > > > > the
> > > > > > > control key?  If so, then how can I get back the selections
from
> > the
> > > > > > listbox?
> > > > > >
> > > > > > Yes. You can use code similar to the code below to get the
selected
> > > > values.
> > > > > >
> > > > > > if (this.listBox1.SelectedItems.Count > 0)
> > > > > > {
> > > > > >   int count = this.listBox1.SelectedItems.Count;
> > > > > >   for (int x = 0; x < count; x++)
> > > > > >   {
> > > > > >     DataRowView rowView = this.listBox1.SelectedItems[x] as
> > DataRowView;
> > > > > >     if (rowView != null)
> > > > > >     {
> > > > > >       MessageBox.Show(rowView.Row["Column1"].ToString());
> > > > > >     }
> > > > > >   }
> > > > > > }
> > > > > >
> > > > > > --
> > > > > > Tim Wilson
> > > > > > ..Net Compact Framework MVP
> > > > > >
> > > > > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > > > > news:AFB42B7D-8884-4FF1-9BBB-6B165F251610@microsoft.com...
> > > > > > > Hi,
> > > > > > > I'm working on a Windows applicaton with VS 2003 on windows
2000.
> > I
> > > > have
> > > > > > a
> > > > > > > listbox that I have binded to a dataset table, "source" which
has
> > 3
> > > > > > columns.
> > > > > > > I would like to display 2 of those columns, "scode" and
"sname",
> > as 1
> > > > > > column
> > > > > > > (if not possible then 2 columns will be fine) in the listbox.
Can
> > the
> > > > > > > listbox display 2 columns information from the dataset and how
can
> > I
> > > > do
> > > > > > that?
> > > > > > >
> > > > > > > Also, I set the property of the listbox to selectionmode to
> > > > MultiExtend.
> > > > > > > Would this allow the user to select multiple rows form the
listbox
> > > > using
> > > > > > the
> > > > > > > control key?  If so, then how can I get back the selections
from
> > the
> > > > > > listbox?
> > > > > > >
> > > > > > > Thanks in advance for any help provided,
> > > > > > > Alpha
> > > > > >
> > > > > >
> > > > > >
> > > >
> > > >
> > > >
> >
> >
> >
Author
31 Mar 2005 10:35 PM
Alpha
I thought I am clearing and disposing the table source or maybe I'm not doing
it correctly?

In the code I paste earlier, this is where I created the table "source":

cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
saLocal.Fill(dsLocal,"source");

This is where I clear and dispose the table:

dsLocal.Tables["source"].Clear();
dsLocal.Tables["source"].Dispose();
dvSource.Dispose();
Show quote
"Tim Wilson" wrote:

> I don't see, in the code provided, where you are creating the DataTable.
> Once you've disposed of the DataTable you should create a new instance of
> it. The new instance can't possibly know about the column that you added to
> the previous instance - they are two separate objects. I would say you have
> two options.
> (1) Simply clear the DataTable of all it's data and leave the empty
> DataTable in the DataSet. Do not dispose of it.
> (2) Remove the DataTable from the DataSet, dispose of it, and then you'll
> need to recreate it, add the new column, and add it back to the DataSet when
> appropriate.
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:6866F615-D1A1-45EF-ADA4-DED2A196BDFD@microsoft.com...
> > I dispose the table that contain the new column that I added (to display 2
> > columns of data on the listbox) when I de-select the radio button.  I get
> an
> > error about ducplicat column existed alreday when I re-select the radio
> > button because my code would create the table and the new column.  Why
> isn't
> > the added column removed when I dispose the table?  How do I get rid of
> it?
> > Thanks for your help, Alpha
> >
> > private void radioPayPlan_CheckedChanged(object sender, System.EventArgs
> e)
> > {
> > try
> > {
> > if (radioPayPlan.Checked == true)
> > {
> > //Setup the PaySource in listbox
> > cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
> > saLocal.Fill(dsLocal,"source");
> >
> > drv = (DataRowView) BindingContext[dsLocal.Tables["source"]].Current;
> > dsLocal.Tables["source"].AcceptChanges();
> > //Create new column
> > DataColumn sc = new
> > DataColumn("ScodeName",Type.GetType("System.String"));
> > sc.Expression = "iif(len(scode) > 0,scode + ': ' + sname,sname)";
> > dsLocal.Tables["source"].Columns.Add(sc);
> > //this.lstSelections.SelectedIndex = 1;
> >
> > dvSource = new
> >
> DataView(dsLocal.Tables["source"],"","sname",DataViewRowState.CurrentRows);
> > this.lstSelections.DataSource = dvSource;
> > lstSelections.DisplayMember = "ScodeName";
> > lstSelections.ValueMember = "sID";
> > }
> > else
> > {
> > dsLocal.Tables["source"].Clear();
> > dsLocal.Tables["source"].Dispose();
> > dvSource.Dispose();
> >
> > }
> > }
> > catch(Exception my_e)
> > {
> > MessageBox.Show("The system encountered an error loading listing
> > data:\n" + my_e.ToString());
> > }
> >
> > }
> >
> >
> > "Tim Wilson" wrote:
> >
> > > Something similar to the code below should work.
> > >
> > > if (this.listBox1.SelectedItems.Count > 0)
> > > {
> > >   int count = this.listBox1.SelectedItems.Count;
> > >   for (int x = 0; x < count; x++)
> > >   {
> > >     DataRowView rowView = this.listBox1.SelectedItems[x] as DataRowView;
> > >     if (rowView != null)
> > >     {
> > >       MessageBox.Show(rowView.Row[this.listBox1.ValueMember]ToString());
> > >     }
> > >   }
> > > }
> > >
> > > --
> > > Tim Wilson
> > > ..Net Compact Framework MVP
> > >
> > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > news:54CC228B-063D-434A-8151-F7063191F61C@microsoft.com...
> > > > I'm back to using the listbox with a newly added column to display the
> 2
> > > > cloumns data from the dataset.  Question, how can I retrieve the
> > > valuemembers
> > > >  from the selected items of the list box?  Many thanks in adavnce,
> Alpha
> > > >
> > > > try
> > > > {
> > > > if (radioAttendant.Checked == true)
> > > > {
> > > > //Setup the PaySource in listbox
> > > > cmdLocal.CommandText = "select eid, ename from tblEmployee" ;
> > > > saLocal.Fill(dsLocal,"attendant");
> > > >
> > > > drv = (DataRowView)
> BindingContext[dsLocal.Tables["attendant"]].Current;
> > > > dvAttendant = new
> > > >
> > >
> DataView(dsLocal.Tables["attendant"],"","ename",DataViewRowState.CurrentRows
> > > );
> > > > this.lstSelections.DataSource = dvAttendant;
> > > > lstSelections.DisplayMember = "ename";
> > > > lstSelections.ValueMember = "eid";
> > > > }
> > > > else
> > > > {
> > > > dsLocal.Tables["attendant"].Clear();
> > > > dsLocal.Tables["attendant"].Dispose();
> > > > dvAttendant.Dispose();
> > > >
> > > > }
> > > > }
> > > > catch(Exception my_e)
> > > > {
> > > > MessageBox.Show("The system encountered an error loading listing
> > > > data:\n" + my_e.ToString());
> > > > }
> > > >
> > > > "Tim Wilson" wrote:
> > > >
> > > > > So you're moving to the DataGrid control? That might make more sense
> if
> > > you
> > > > > need to do multi-column data display from a data source.
> > > > >
> > > > > --
> > > > > Tim Wilson
> > > > > ..Net Compact Framework MVP
> > > > >
> > > > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > > > news:73286AF6-2F6E-4842-B0FE-9B66980CC89C@microsoft.com...
> > > > > > I thought about doing that but wonder if there is a property
> setting
> > > or
> > > > > form
> > > > > > code to do that quick.  I now think that it might be better to use
> the
> > > > > > datagrid for listing.  I change the listing display/content
> depending
> > > on
> > > > > > which one of the 8 radio buttons that the user selects.  Is it
> easier
> > > to
> > > > > do
> > > > > > multicolumn display and getting the multi-selections back by using
> the
> > > > > > datagrid control instead.
> > > > > >
> > > > > > Thanks,
> > > > > > Alpha
> > > > > >
> > > > > > "Tim Wilson" wrote:
> > > > > >
> > > > > > > > I'm working on a Windows applicaton with VS 2003 on windows
> 2000.
> > > I
> > > > > have
> > > > > > > a
> > > > > > > > listbox that I have binded to a dataset table, "source" which
> has
> > > 3
> > > > > > > columns.
> > > > > > > > I would like to display 2 of those columns, "scode" and
> "sname",
> > > as 1
> > > > > > > column
> > > > > > > > (if not possible then 2 columns will be fine) in the listbox.
> Can
> > > the
> > > > > > > > listbox display 2 columns information from the dataset and how
> can
> > > I
> > > > > do
> > > > > > > that?
> > > > > > >
> > > > > > > You could add another column to the DataTable that will combine
> the
> > > data
> > > > > of
> > > > > > > the other two columns into one.
> > > > > > >
> > > > > > > private DataTable dataTable1;
> > > > > > >
> > > > > > > ....
> > > > > > >
> > > > > > > dataTable1 = new DataTable("MyTable");
> > > > > > > dataTable1.Columns.Add("Column1", typeof(string));
> > > > > > > dataTable1.Columns.Add("Column2", typeof(string));
> > > > > > > dataTable1.Columns.Add("Column3", typeof(string));
> > > > > > > dataTable1.Columns.Add("Column1Column2",
> typeof(string)).Expression
> > > =
> > > > > > > "Column1 + ', ' + Column2";
> > > > > > >
> > > > > > > for (int x = 0; x < 25; x++)
> > > > > > > {
> > > > > > >   dataTable1.Rows.Add(new object[] {"FirstName" + x.ToString(),
> > > > > "LastName" +
> > > > > > > x.ToString(), "Nothing", null});
> > > > > > > }
> > > > > > >
> > > > > > > this.listBox1.DisplayMember = "Column1Column2";
> > > > > > > this.listBox1.DataSource = dataTable1;
> > > > > > >
> > > > > > > > Also, I set the property of the listbox to selectionmode to
> > > > > MultiExtend.
> > > > > > > > Would this allow the user to select multiple rows form the
> listbox
> > > > > using
> > > > > > > the
> > > > > > > > control key?  If so, then how can I get back the selections
> from
> > > the
> > > > > > > listbox?
> > > > > > >
> > > > > > > Yes. You can use code similar to the code below to get the
> selected
> > > > > values.
> > > > > > >
> > > > > > > if (this.listBox1.SelectedItems.Count > 0)
> > > > > > > {
> > > > > > >   int count = this.listBox1.SelectedItems.Count;
> > > > > > >   for (int x = 0; x < count; x++)
> > > > > > >   {
> > > > > > >     DataRowView rowView = this.listBox1.SelectedItems[x] as
> > > DataRowView;
> > > > > > >     if (rowView != null)
> > > > > > >     {
> > > > > > >       MessageBox.Show(rowView.Row["Column1"].ToString());
> > > > > > >     }
> > > > > > >   }
> > > > > > > }
> > > > > > >
> > > > > > > --
> > > > > > > Tim Wilson
> > > > > > > ..Net Compact Framework MVP
> > > > > > >
> > > > > > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > > > > > news:AFB42B7D-8884-4FF1-9BBB-6B165F251610@microsoft.com...
> > > > > > > > Hi,
> > > > > > > > I'm working on a Windows applicaton with VS 2003 on windows
> 2000.
> > > I
> > > > > have
> > > > > > > a
> > > > > > > > listbox that I have binded to a dataset table, "source" which
> has
> > > 3
> > > > > > > columns.
> > > > > > > > I would like to display 2 of those columns, "scode" and
> "sname",
> > > as 1
> > > > > > > column
> > > > > > > > (if not possible then 2 columns will be fine) in the listbox.
> Can
> > > the
> > > > > > > > listbox display 2 columns information from the dataset and how
> can
> > > I
> > > > > do
> > > > > > > that?
> > > > > > > >
> > > > > > > > Also, I set the property of the listbox to selectionmode to
> > > > > MultiExtend.
> > > > > > > > Would this allow the user to select multiple rows form the
> listbox
> > > > > using
> > > > > > > the
> > > > > > > > control key?  If so, then how can I get back the selections
> from
> > > the
> > > > > > > listbox?
> > > > > > > >
> > > > > > > > Thanks in advance for any help provided,
> > > > > > > > Alpha
> > > > > > >
> > > > > > >
> > > > > > >
> > > > >
> > > > >
> > > > >
> > >
> > >
> > >
>
>
>
Author
31 Mar 2005 11:05 PM
Tim Wilson
I believe that the data adapters Fill() method will only create the
DataTable if it does not already exist. Calling Dispose() marks the object
for removal but does not necessarily remove it. So I would say remove the
DataTable from the DataSet, then Dispose() it. Try this and see if it helps.

DataTable dt = dsLocal.Tables["source"];
dsLocal.Tables.Remove(dt);
dt.Dispose();

--
Tim Wilson
..Net Compact Framework MVP

Show quote
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:4D50AE68-C30B-47FC-8D58-2C09E9177D23@microsoft.com...
> I thought I am clearing and disposing the table source or maybe I'm not
doing
> it correctly?
>
> In the code I paste earlier, this is where I created the table "source":
>
> cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
> saLocal.Fill(dsLocal,"source");
>
> This is where I clear and dispose the table:
>
> dsLocal.Tables["source"].Clear();
> dsLocal.Tables["source"].Dispose();
> dvSource.Dispose();
Author
31 Mar 2005 11:59 PM
Alpha
I got an error message "System.NullReferenceException:object referrence not
set to an instance of an object".  This is the code I entered.  I stepped
through the program and the exception occurs when it hits the 2nd line of
code, the Remove. 

DataTable dt = dsLocal.Tables["source"];
dsLocal.Tables.Remove(dt);
dsLocal.Tables["source"].Dispose();
dvSource.Dispose();



Show quote
"Tim Wilson" wrote:

> I believe that the data adapters Fill() method will only create the
> DataTable if it does not already exist. Calling Dispose() marks the object
> for removal but does not necessarily remove it. So I would say remove the
> DataTable from the DataSet, then Dispose() it. Try this and see if it helps.
>
> DataTable dt = dsLocal.Tables["source"];
> dsLocal.Tables.Remove(dt);
> dt.Dispose();
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:4D50AE68-C30B-47FC-8D58-2C09E9177D23@microsoft.com...
> > I thought I am clearing and disposing the table source or maybe I'm not
> doing
> > it correctly?
> >
> > In the code I paste earlier, this is where I created the table "source":
> >
> > cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
> > saLocal.Fill(dsLocal,"source");
> >
> > This is where I clear and dispose the table:
> >
> > dsLocal.Tables["source"].Clear();
> > dsLocal.Tables["source"].Dispose();
> > dvSource.Dispose();
>
>
>
Author
1 Apr 2005 12:14 AM
Tim Wilson
Are you sure that the DataSet contains a DataTable named "source" at that
point? You can check with the following code.
if (dsLocal.Tables.Contains("source"))
{
  ...
}
If the DataSet contains a DataTable named "source" then the first line will
return a reference to it. The second line will then remove the DataTable
from the DataSet based on the reference. The third line, that you have, will
then attempt to locate the DataTable, which you just removed, and call its
Dispose() method. If the DataSet does contains a DataTable named "source",
are you sure that the exception doesn't happen on this line?
"dsLocal.Tables["source"].Dispose();"

--
Tim Wilson
..Net Compact Framework MVP

Show quote
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:80B8C807-4975-4DE8-B4EF-BC71B5D4F665@microsoft.com...
> I got an error message "System.NullReferenceException:object referrence
not
> set to an instance of an object".  This is the code I entered.  I stepped
> through the program and the exception occurs when it hits the 2nd line of
> code, the Remove.
>
> DataTable dt = dsLocal.Tables["source"];
> dsLocal.Tables.Remove(dt);
> dsLocal.Tables["source"].Dispose();
> dvSource.Dispose();
>
>
>
> "Tim Wilson" wrote:
>
> > I believe that the data adapters Fill() method will only create the
> > DataTable if it does not already exist. Calling Dispose() marks the
object
> > for removal but does not necessarily remove it. So I would say remove
the
> > DataTable from the DataSet, then Dispose() it. Try this and see if it
helps.
> >
> > DataTable dt = dsLocal.Tables["source"];
> > dsLocal.Tables.Remove(dt);
> > dt.Dispose();
> >
> > --
> > Tim Wilson
> > ..Net Compact Framework MVP
> >
> > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > news:4D50AE68-C30B-47FC-8D58-2C09E9177D23@microsoft.com...
> > > I thought I am clearing and disposing the table source or maybe I'm
not
> > doing
> > > it correctly?
> > >
> > > In the code I paste earlier, this is where I created the table
"source":
> > >
> > > cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
> > > saLocal.Fill(dsLocal,"source");
> > >
> > > This is where I clear and dispose the table:
> > >
> > > dsLocal.Tables["source"].Clear();
> > > dsLocal.Tables["source"].Dispose();
> > > dvSource.Dispose();
> >
> >
> >
Author
1 Apr 2005 12:39 AM
Alpha
Yes the table is there.  As I step through the program it went into the
section where the "source' table exists and still printing out the error.  I
thought the "saLocal.Fill(dsLocal,"source");" creates the table, right?  Well
it's there in my code but I just can't seem to clean it up when I want to. 
I'll just test to see if the table exist and won't recreate it and then
dispose the dataset and tables when this module exists.  Thank you very much
for your help.  If you have other suggestions then I'll try it again.

if (dsLocal.Tables.Contains("source"))
{
dsLocal.Tables["source"].Clear();
DataTable dt = dsLocal.Tables["source"];
dsLocal.Tables.Remove(dt);
dsLocal.Tables["source"].Dispose();
dvSource.Dispose();
}
else
{}


Show quote
"Tim Wilson" wrote:

> Are you sure that the DataSet contains a DataTable named "source" at that
> point? You can check with the following code.
> if (dsLocal.Tables.Contains("source"))
> {
>   ...
> }
> If the DataSet contains a DataTable named "source" then the first line will
> return a reference to it. The second line will then remove the DataTable
> from the DataSet based on the reference. The third line, that you have, will
> then attempt to locate the DataTable, which you just removed, and call its
> Dispose() method. If the DataSet does contains a DataTable named "source",
> are you sure that the exception doesn't happen on this line?
> "dsLocal.Tables["source"].Dispose();"
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:80B8C807-4975-4DE8-B4EF-BC71B5D4F665@microsoft.com...
> > I got an error message "System.NullReferenceException:object referrence
> not
> > set to an instance of an object".  This is the code I entered.  I stepped
> > through the program and the exception occurs when it hits the 2nd line of
> > code, the Remove.
> >
> > DataTable dt = dsLocal.Tables["source"];
> > dsLocal.Tables.Remove(dt);
> > dsLocal.Tables["source"].Dispose();
> > dvSource.Dispose();
> >
> >
> >
> > "Tim Wilson" wrote:
> >
> > > I believe that the data adapters Fill() method will only create the
> > > DataTable if it does not already exist. Calling Dispose() marks the
> object
> > > for removal but does not necessarily remove it. So I would say remove
> the
> > > DataTable from the DataSet, then Dispose() it. Try this and see if it
> helps.
> > >
> > > DataTable dt = dsLocal.Tables["source"];
> > > dsLocal.Tables.Remove(dt);
> > > dt.Dispose();
> > >
> > > --
> > > Tim Wilson
> > > ..Net Compact Framework MVP
> > >
> > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > news:4D50AE68-C30B-47FC-8D58-2C09E9177D23@microsoft.com...
> > > > I thought I am clearing and disposing the table source or maybe I'm
> not
> > > doing
> > > > it correctly?
> > > >
> > > > In the code I paste earlier, this is where I created the table
> "source":
> > > >
> > > > cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
> > > > saLocal.Fill(dsLocal,"source");
> > > >
> > > > This is where I clear and dispose the table:
> > > >
> > > > dsLocal.Tables["source"].Clear();
> > > > dsLocal.Tables["source"].Dispose();
> > > > dvSource.Dispose();
> > >
> > >
> > >
>
>
>
Author
1 Apr 2005 12:57 AM
Alpha
I commented out the "dsLocal.Tables["source"].Dispose();", the line after the
remove, "dsLocal.Tables.Remove(dt);" and the error message stopped.  I guess
you can't do them both and I still am not sure what the differences are
between these 2. 

Thanks,
Alpha

Show quote
"Tim Wilson" wrote:

> Are you sure that the DataSet contains a DataTable named "source" at that
> point? You can check with the following code.
> if (dsLocal.Tables.Contains("source"))
> {
>   ...
> }
> If the DataSet contains a DataTable named "source" then the first line will
> return a reference to it. The second line will then remove the DataTable
> from the DataSet based on the reference. The third line, that you have, will
> then attempt to locate the DataTable, which you just removed, and call its
> Dispose() method. If the DataSet does contains a DataTable named "source",
> are you sure that the exception doesn't happen on this line?
> "dsLocal.Tables["source"].Dispose();"
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:80B8C807-4975-4DE8-B4EF-BC71B5D4F665@microsoft.com...
> > I got an error message "System.NullReferenceException:object referrence
> not
> > set to an instance of an object".  This is the code I entered.  I stepped
> > through the program and the exception occurs when it hits the 2nd line of
> > code, the Remove.
> >
> > DataTable dt = dsLocal.Tables["source"];
> > dsLocal.Tables.Remove(dt);
> > dsLocal.Tables["source"].Dispose();
> > dvSource.Dispose();
> >
> >
> >
> > "Tim Wilson" wrote:
> >
> > > I believe that the data adapters Fill() method will only create the
> > > DataTable if it does not already exist. Calling Dispose() marks the
> object
> > > for removal but does not necessarily remove it. So I would say remove
> the
> > > DataTable from the DataSet, then Dispose() it. Try this and see if it
> helps.
> > >
> > > DataTable dt = dsLocal.Tables["source"];
> > > dsLocal.Tables.Remove(dt);
> > > dt.Dispose();
> > >
> > > --
> > > Tim Wilson
> > > ..Net Compact Framework MVP
> > >
> > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > news:4D50AE68-C30B-47FC-8D58-2C09E9177D23@microsoft.com...
> > > > I thought I am clearing and disposing the table source or maybe I'm
> not
> > > doing
> > > > it correctly?
> > > >
> > > > In the code I paste earlier, this is where I created the table
> "source":
> > > >
> > > > cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
> > > > saLocal.Fill(dsLocal,"source");
> > > >
> > > > This is where I clear and dispose the table:
> > > >
> > > > dsLocal.Tables["source"].Clear();
> > > > dsLocal.Tables["source"].Dispose();
> > > > dvSource.Dispose();
> > >
> > >
> > >
>
>
>
Author
1 Apr 2005 3:38 AM
Tim Wilson
The reason you are getting the error is because you are trying to retrieve
the "source" table from the Tables collection AFTER you have removed it. So
the code below is what you are doing now.

DataTable dt = dsLocal.Tables["source"];
dsLocal.Tables.Remove(dt);
dsLocal.Tables["source"].Dispose();

You get the exception because "dsLocal.Tables["source"]" will return null,
in the last line, since you have removed it from the Tables collection. And
then you are trying to call Dispose() on this null reference that is
returned - NullReferenceException.

This is the code that you should be using.

DataTable dt = dsLocal.Tables["source"];
dsLocal.Tables.Remove(dt);
dt.Dispose();

--
Tim Wilson
..Net Compact Framework MVP

Show quote
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:9D5FFAD4-AAC7-42F7-9FE6-8E8ABAFBBF88@microsoft.com...
> I commented out the "dsLocal.Tables["source"].Dispose();", the line after
the
> remove, "dsLocal.Tables.Remove(dt);" and the error message stopped.  I
guess
> you can't do them both and I still am not sure what the differences are
> between these 2.
>
> Thanks,
> Alpha
>
> "Tim Wilson" wrote:
>
> > Are you sure that the DataSet contains a DataTable named "source" at
that
> > point? You can check with the following code.
> > if (dsLocal.Tables.Contains("source"))
> > {
> >   ...
> > }
> > If the DataSet contains a DataTable named "source" then the first line
will
> > return a reference to it. The second line will then remove the DataTable
> > from the DataSet based on the reference. The third line, that you have,
will
> > then attempt to locate the DataTable, which you just removed, and call
its
> > Dispose() method. If the DataSet does contains a DataTable named
"source",
> > are you sure that the exception doesn't happen on this line?
> > "dsLocal.Tables["source"].Dispose();"
> >
> > --
> > Tim Wilson
> > ..Net Compact Framework MVP
> >
> > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > news:80B8C807-4975-4DE8-B4EF-BC71B5D4F665@microsoft.com...
> > > I got an error message "System.NullReferenceException:object
referrence
> > not
> > > set to an instance of an object".  This is the code I entered.  I
stepped
> > > through the program and the exception occurs when it hits the 2nd line
of
> > > code, the Remove.
> > >
> > > DataTable dt = dsLocal.Tables["source"];
> > > dsLocal.Tables.Remove(dt);
> > > dsLocal.Tables["source"].Dispose();
> > > dvSource.Dispose();
> > >
> > >
> > >
> > > "Tim Wilson" wrote:
> > >
> > > > I believe that the data adapters Fill() method will only create the
> > > > DataTable if it does not already exist. Calling Dispose() marks the
> > object
> > > > for removal but does not necessarily remove it. So I would say
remove
> > the
> > > > DataTable from the DataSet, then Dispose() it. Try this and see if
it
> > helps.
> > > >
> > > > DataTable dt = dsLocal.Tables["source"];
> > > > dsLocal.Tables.Remove(dt);
> > > > dt.Dispose();
> > > >
> > > > --
> > > > Tim Wilson
> > > > ..Net Compact Framework MVP
> > > >
> > > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > > news:4D50AE68-C30B-47FC-8D58-2C09E9177D23@microsoft.com...
> > > > > I thought I am clearing and disposing the table source or maybe
I'm
> > not
> > > > doing
> > > > > it correctly?
> > > > >
> > > > > In the code I paste earlier, this is where I created the table
> > "source":
> > > > >
> > > > > cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
> > > > > saLocal.Fill(dsLocal,"source");
> > > > >
> > > > > This is where I clear and dispose the table:
> > > > >
> > > > > dsLocal.Tables["source"].Clear();
> > > > > dsLocal.Tables["source"].Dispose();
> > > > > dvSource.Dispose();
> > > >
> > > >
> > > >
> >
> >
> >
Author
1 Apr 2005 5:51 PM
Alpha
Yes, that works beautifully!   I see what I did wrong now.  Thank you so much
and have a great day!

Alpha

Show quote
"Tim Wilson" wrote:

> The reason you are getting the error is because you are trying to retrieve
> the "source" table from the Tables collection AFTER you have removed it. So
> the code below is what you are doing now.
>
> DataTable dt = dsLocal.Tables["source"];
> dsLocal.Tables.Remove(dt);
> dsLocal.Tables["source"].Dispose();
>
> You get the exception because "dsLocal.Tables["source"]" will return null,
> in the last line, since you have removed it from the Tables collection. And
> then you are trying to call Dispose() on this null reference that is
> returned - NullReferenceException.
>
> This is the code that you should be using.
>
> DataTable dt = dsLocal.Tables["source"];
> dsLocal.Tables.Remove(dt);
> dt.Dispose();
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:9D5FFAD4-AAC7-42F7-9FE6-8E8ABAFBBF88@microsoft.com...
> > I commented out the "dsLocal.Tables["source"].Dispose();", the line after
> the
> > remove, "dsLocal.Tables.Remove(dt);" and the error message stopped.  I
> guess
> > you can't do them both and I still am not sure what the differences are
> > between these 2.
> >
> > Thanks,
> > Alpha
> >
> > "Tim Wilson" wrote:
> >
> > > Are you sure that the DataSet contains a DataTable named "source" at
> that
> > > point? You can check with the following code.
> > > if (dsLocal.Tables.Contains("source"))
> > > {
> > >   ...
> > > }
> > > If the DataSet contains a DataTable named "source" then the first line
> will
> > > return a reference to it. The second line will then remove the DataTable
> > > from the DataSet based on the reference. The third line, that you have,
> will
> > > then attempt to locate the DataTable, which you just removed, and call
> its
> > > Dispose() method. If the DataSet does contains a DataTable named
> "source",
> > > are you sure that the exception doesn't happen on this line?
> > > "dsLocal.Tables["source"].Dispose();"
> > >
> > > --
> > > Tim Wilson
> > > ..Net Compact Framework MVP
> > >
> > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > news:80B8C807-4975-4DE8-B4EF-BC71B5D4F665@microsoft.com...
> > > > I got an error message "System.NullReferenceException:object
> referrence
> > > not
> > > > set to an instance of an object".  This is the code I entered.  I
> stepped
> > > > through the program and the exception occurs when it hits the 2nd line
> of
> > > > code, the Remove.
> > > >
> > > > DataTable dt = dsLocal.Tables["source"];
> > > > dsLocal.Tables.Remove(dt);
> > > > dsLocal.Tables["source"].Dispose();
> > > > dvSource.Dispose();
> > > >
> > > >
> > > >
> > > > "Tim Wilson" wrote:
> > > >
> > > > > I believe that the data adapters Fill() method will only create the
> > > > > DataTable if it does not already exist. Calling Dispose() marks the
> > > object
> > > > > for removal but does not necessarily remove it. So I would say
> remove
> > > the
> > > > > DataTable from the DataSet, then Dispose() it. Try this and see if
> it
> > > helps.
> > > > >
> > > > > DataTable dt = dsLocal.Tables["source"];
> > > > > dsLocal.Tables.Remove(dt);
> > > > > dt.Dispose();
> > > > >
> > > > > --
> > > > > Tim Wilson
> > > > > ..Net Compact Framework MVP
> > > > >
> > > > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > > > news:4D50AE68-C30B-47FC-8D58-2C09E9177D23@microsoft.com...
> > > > > > I thought I am clearing and disposing the table source or maybe
> I'm
> > > not
> > > > > doing
> > > > > > it correctly?
> > > > > >
> > > > > > In the code I paste earlier, this is where I created the table
> > > "source":
> > > > > >
> > > > > > cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
> > > > > > saLocal.Fill(dsLocal,"source");
> > > > > >
> > > > > > This is where I clear and dispose the table:
> > > > > >
> > > > > > dsLocal.Tables["source"].Clear();
> > > > > > dsLocal.Tables["source"].Dispose();
> > > > > > dvSource.Dispose();
> > > > >
> > > > >
> > > > >
> > >
> > >
> > >
>
>
>
Author
1 Apr 2005 6:15 PM
Tim Wilson
You're welcome.

--
Tim Wilson
..Net Compact Framework MVP

Show quote
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:192AC2D5-2BED-4717-B95D-6CB062B72F10@microsoft.com...
> Yes, that works beautifully!   I see what I did wrong now.  Thank you so
much
> and have a great day!
>
> Alpha
>
> "Tim Wilson" wrote:
>
> > The reason you are getting the error is because you are trying to
retrieve
> > the "source" table from the Tables collection AFTER you have removed it.
So
> > the code below is what you are doing now.
> >
> > DataTable dt = dsLocal.Tables["source"];
> > dsLocal.Tables.Remove(dt);
> > dsLocal.Tables["source"].Dispose();
> >
> > You get the exception because "dsLocal.Tables["source"]" will return
null,
> > in the last line, since you have removed it from the Tables collection.
And
> > then you are trying to call Dispose() on this null reference that is
> > returned - NullReferenceException.
> >
> > This is the code that you should be using.
> >
> > DataTable dt = dsLocal.Tables["source"];
> > dsLocal.Tables.Remove(dt);
> > dt.Dispose();
> >
> > --
> > Tim Wilson
> > ..Net Compact Framework MVP
> >
> > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > news:9D5FFAD4-AAC7-42F7-9FE6-8E8ABAFBBF88@microsoft.com...
> > > I commented out the "dsLocal.Tables["source"].Dispose();", the line
after
> > the
> > > remove, "dsLocal.Tables.Remove(dt);" and the error message stopped.  I
> > guess
> > > you can't do them both and I still am not sure what the differences
are
> > > between these 2.
> > >
> > > Thanks,
> > > Alpha
> > >
> > > "Tim Wilson" wrote:
> > >
> > > > Are you sure that the DataSet contains a DataTable named "source" at
> > that
> > > > point? You can check with the following code.
> > > > if (dsLocal.Tables.Contains("source"))
> > > > {
> > > >   ...
> > > > }
> > > > If the DataSet contains a DataTable named "source" then the first
line
> > will
> > > > return a reference to it. The second line will then remove the
DataTable
> > > > from the DataSet based on the reference. The third line, that you
have,
> > will
> > > > then attempt to locate the DataTable, which you just removed, and
call
> > its
> > > > Dispose() method. If the DataSet does contains a DataTable named
> > "source",
> > > > are you sure that the exception doesn't happen on this line?
> > > > "dsLocal.Tables["source"].Dispose();"
> > > >
> > > > --
> > > > Tim Wilson
> > > > ..Net Compact Framework MVP
> > > >
> > > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > > news:80B8C807-4975-4DE8-B4EF-BC71B5D4F665@microsoft.com...
> > > > > I got an error message "System.NullReferenceException:object
> > referrence
> > > > not
> > > > > set to an instance of an object".  This is the code I entered.  I
> > stepped
> > > > > through the program and the exception occurs when it hits the 2nd
line
> > of
> > > > > code, the Remove.
> > > > >
> > > > > DataTable dt = dsLocal.Tables["source"];
> > > > > dsLocal.Tables.Remove(dt);
> > > > > dsLocal.Tables["source"].Dispose();
> > > > > dvSource.Dispose();
> > > > >
> > > > >
> > > > >
> > > > > "Tim Wilson" wrote:
> > > > >
> > > > > > I believe that the data adapters Fill() method will only create
the
> > > > > > DataTable if it does not already exist. Calling Dispose() marks
the
> > > > object
> > > > > > for removal but does not necessarily remove it. So I would say
> > remove
> > > > the
> > > > > > DataTable from the DataSet, then Dispose() it. Try this and see
if
> > it
> > > > helps.
> > > > > >
> > > > > > DataTable dt = dsLocal.Tables["source"];
> > > > > > dsLocal.Tables.Remove(dt);
> > > > > > dt.Dispose();
> > > > > >
> > > > > > --
> > > > > > Tim Wilson
> > > > > > ..Net Compact Framework MVP
> > > > > >
> > > > > > "Alpha" <Al***@discussions.microsoft.com> wrote in message
> > > > > > news:4D50AE68-C30B-47FC-8D58-2C09E9177D23@microsoft.com...
> > > > > > > I thought I am clearing and disposing the table source or
maybe
> > I'm
> > > > not
> > > > > > doing
> > > > > > > it correctly?
> > > > > > >
> > > > > > > In the code I paste earlier, this is where I created the table
> > > > "source":
> > > > > > >
> > > > > > > cmdLocal.CommandText = "select sID, scode, sname from
tblsource" ;
> > > > > > > saLocal.Fill(dsLocal,"source");
> > > > > > >
> > > > > > > This is where I clear and dispose the table:
> > > > > > >
> > > > > > > dsLocal.Tables["source"].Clear();
> > > > > > > dsLocal.Tables["source"].Dispose();
> > > > > > > dvSource.Dispose();
> > > > > >
> > > > > >
> > > > > >
> > > >
> > > >
> > > >
> >
> >
> >
Author
1 Apr 2005 12:05 AM
Alpha
I got an error "System.NullReferenceException: Object refernce not set to an
instance of an object."  This occurs on the Remove when I step trhough the
program. 

Maybe I should just keep the table once it's created.  How do I test if a
table exists in the dataset already?  All tables will be disposed with the
dataset when the user exits this module.

DataTable dt = dsLocal.Tables["source"];
dsLocal.Tables.Remove(dt);
dsLocal.Tables["source"].Dispose();
dvSource.Dispose();


Show quote
"Tim Wilson" wrote:

> I believe that the data adapters Fill() method will only create the
> DataTable if it does not already exist. Calling Dispose() marks the object
> for removal but does not necessarily remove it. So I would say remove the
> DataTable from the DataSet, then Dispose() it. Try this and see if it helps.
>
> DataTable dt = dsLocal.Tables["source"];
> dsLocal.Tables.Remove(dt);
> dt.Dispose();
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:4D50AE68-C30B-47FC-8D58-2C09E9177D23@microsoft.com...
> > I thought I am clearing and disposing the table source or maybe I'm not
> doing
> > it correctly?
> >
> > In the code I paste earlier, this is where I created the table "source":
> >
> > cmdLocal.CommandText = "select sID, scode, sname from tblsource" ;
> > saLocal.Fill(dsLocal,"source");
> >
> > This is where I clear and dispose the table:
> >
> > dsLocal.Tables["source"].Clear();
> > dsLocal.Tables["source"].Dispose();
> > dvSource.Dispose();
>
>
>

AddThis Social Bookmark Button