By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,830 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,830 IT Pros & Developers. It's quick & easy.

How to display multicolumns and get back multi selections form lis

P: n/a
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
Share this Question
Share on Google+
18 Replies


P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.