How to display multicolumns and get back multi selections form lis

G

Guest

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
 
T

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());
}
}
}
 
G

Guest

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 said:
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 said:
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
 
T

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

Alpha said:
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 said:
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 said:
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
 
G

Guest

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

Alpha

Tim Wilson said:
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 said:
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 said:
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

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
 
G

Guest

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 said:
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 said:
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 said:
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

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
 
T

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

Alpha said:
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 said:
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 said:
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

:

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

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
 
G

Guest

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 said:
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 said:
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 said:
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

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

:

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

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
 
T

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

Alpha said:
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 said:
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 said:
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());
}

:

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

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

:

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

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
 
G

Guest

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 said:
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 said:
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 said:
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

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());
}

:

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

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

:

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

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
 
T

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();
 
G

Guest

I got an error message "System.NullReferenceException:blush:bject 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 said:
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 said:
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();
 
G

Guest

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 said:
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 said:
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();
 
T

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

Alpha said:
I got an error message "System.NullReferenceException:blush:bject 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 said:
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 said:
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();
 
G

Guest

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 said:
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 said:
I got an error message "System.NullReferenceException:blush:bject 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 said:
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

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();
 
G

Guest

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 said:
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 said:
I got an error message "System.NullReferenceException:blush:bject 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 said:
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

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();
 
T

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

Alpha said:
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 said:
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 said:
I got an error message "System.NullReferenceException:blush:bject
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();



:

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

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();
 
G

Guest

Yes, that works beautifully! I see what I did wrong now. Thank you so much
and have a great day!

Alpha

Tim Wilson said:
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 said:
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 said:
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

I got an error message "System.NullReferenceException:blush:bject 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();



:

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

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();
 
T

Tim Wilson

You're welcome.

--
Tim Wilson
..Net Compact Framework MVP

Alpha said:
Yes, that works beautifully! I see what I did wrong now. Thank you so much
and have a great day!

Alpha

Tim Wilson said:
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 said:
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

:

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

I got an error message "System.NullReferenceException:blush:bject 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();



:

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

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();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top