473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to display multicolumns and get back multi selections form lis

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
Nov 17 '05 #1
18 3310
> 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:AF**********************************@microsof t.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

Nov 17 '05 #2
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:AF**********************************@microsof t.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


Nov 17 '05 #3
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:73**********************************@microsof t.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:AF**********************************@microsof t.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


Nov 17 '05 #4
Yes, I'm working on it now. Thank you very much for your help and have a
great weekend.

Alpha

"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:73**********************************@microsof t.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:AF**********************************@microsof t.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


Nov 17 '05 #5
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:73**********************************@microsof t.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:AF**********************************@microsof t.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


Nov 17 '05 #6
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:54**********************************@microsof t.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:73**********************************@microsof t.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:AF**********************************@microsof t.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
>
>
>


Nov 17 '05 #7
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:54**********************************@microsof t.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:73**********************************@microsof t.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:AF**********************************@microsof t.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
> >
> >
> >


Nov 17 '05 #8
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:68**********************************@microsof t.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:54**********************************@microsof t.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:73**********************************@microsof t.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:AF**********************************@microsof t.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
> > >
> > >
> > >
>
>
>


Nov 17 '05 #9
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();
"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:68**********************************@microsof t.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:54**********************************@microsof t.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:73**********************************@microsof t.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:AF**********************************@microsof t.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
> > > >
> > > >
> > > >
> >
> >
> >


Nov 17 '05 #10
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:4D**********************************@microsof t.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();

Nov 17 '05 #11
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:4D**********************************@microsof t.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();


Nov 17 '05 #12
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();
"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:4D**********************************@microsof t.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();


Nov 17 '05 #13
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:80**********************************@microsof t.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:4D**********************************@microsof t.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();


Nov 17 '05 #14
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
{}
"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:80**********************************@microsof t.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:4D**********************************@microsof t.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();


Nov 17 '05 #15
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:80**********************************@microsof t.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:4D**********************************@microsof t.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();


Nov 17 '05 #16
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:9D**********************************@microsof t.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:80**********************************@microsof t.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:4D**********************************@microsof t.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();
>
>
>


Nov 17 '05 #17
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:9D**********************************@microsof t.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:80**********************************@microsof t.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:4D**********************************@microsof t.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();
> >
> >
> >


Nov 17 '05 #18
You're welcome.

--
Tim Wilson
..Net Compact Framework MVP

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.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:9D**********************************@microsof t.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:80**********************************@microsof t.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:4D**********************************@microsof t.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();
> > >
> > >
> > >
>
>
>


Nov 17 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Billy Boone | last post by:
I have a web form that allows the user to enter several pieces of information. They can then click to see the results (which takes them to a report form). From this form, if the user clicks the...
3
by: Iain Hallam | last post by:
Hi. I've been using display:none on the style property of some <option> elements in my forms, which works fine with Mozilla - as expected it removes the option from my dropdown (although it...
18
by: Neil | last post by:
I am using SQL 7 with an MS Access 2000 MDB front end, using bound forms with ODBC linked tables. In one form, the user needs to be able to check a box to select one or more records. This is...
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
3
by: google | last post by:
Hello, I am trying to create a listbox that users can select multiple entries on. I want Access to put each on of those selections in a different row on a particular table. This table will also...
4
by: Noa | last post by:
Hi, Is there a way to use OpenFileDialog1 in order to multi-select folders? I saw in msdn site a sample of multi-file-selections. I need to select multiple folders and to apply an algorithm on...
6
by: probashi | last post by:
Hi, Issue: After post back selected item of a list box is getting out of focus (when it contains more items than it's size). I have a List Box in an ASPX page. I select an item from the...
3
by: phil67b | last post by:
Hello everybody, I have a page rech.php where I'm doing a multi-criteria research Ex. choose your car model, choose your country. After validation of my form, on the same page, the lines will be...
12
by: micarl | last post by:
How would i print a report based on criteria selected from several Combo Boxes as well as multiple Multi Select List Boxes, that are located on the same form? I can get one Multi List Box, just...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.