//Step #1 bind the data:
public bool ListAllowedSources(int xUserID,
System.Web.UI.WebControls.ListBox xListbox)
{
bool result = false;
SqlCommand _MyCommand = null;
SqlDataReader _MyReader = null;
try
{
if ( _MyConnection.State == ConnectionState.Closed ) {
_MyConnection.Open(); }
_MyCommand = new SqlCommand("Table001_Sproc001", _MyConnection);
_MyCommand.CommandType = CommandType.StoredProcedure;
_MyCommand.Parameters.Add(new SqlParameter("@Field01", xUserID));
_MyReader = _MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
xListbox.DataSource = _MyReader;
xListbox.DataTextField = "SourceName";
xListbox.DataValueField = "SourceID";
xListbox.DataBind();
if ( xListbox.Items.Count > 0 ) { xListbox.SelectedIndex = 0; }
result = true;
}
catch (Exception ex)
{
clsErrorsIO myError = new clsErrorsIO();
myError.ErrorAdd(modName,myName, "UID = " + xUserID,ex.Message,false);
}
finally
{
if (_MyCommand != null) { _MyCommand.Dispose(); }
if (!_MyReader.IsClosed) { _MyReader.Close(); }
if (_MyConnection.State != ConnectionState.Closed ) {
_MyConnection.Close(); }
}
return result;
}
//end Step#1
//Step#2 get what was selected from the list:
//
//the foreach is the offending line of code because ctlLB.Items.Count always
== 0, when it should equal the number of items in the list, thus the "if (
li.Selected == true )" never evaluated.
//
foreach(ListItem li in ctlLB.Items)
{
if ( li.Selected == true )
{
ctlTypesIndex = ctlTypesIndex + mm.ToString() + "-";
}
}
>> In code behind,
>
[quoted text clipped - 26 lines]
>>
>> Thank you for your help!
Liz - 24 Nov 2007 07:28 GMT
why: foreach (ListItem li in ctlLB.Items) when the ListBox is xListbox?
shouldn't that be:
foreach (ListItem li in xListbox.Items) ?
where does ctlLB get into the picture?
other than that, I don't see any problems ...
> //Step #1 bind the data:
> public bool ListAllowedSources(int xUserID,
[quoted text clipped - 83 lines]
>>>
>>> Thank you for your help!
Liz - 24 Nov 2007 07:43 GMT
> why: foreach (ListItem li in ctlLB.Items) when the ListBox is xListbox?
>
[quoted text clipped - 5 lines]
>
> other than that, I don't see any problems ...
sorry, I see you passed CtlLB to the ListAllowedSources method ... but I
don't know where in the code your foreach iteration of ctlLB is .. maybe
you're pointing to the wrong reference, or at the wrong time ?? hard to
follow snippets like this ... but I'll bet you can iterate xListbox in the
ListAllowedSources method ... if so, a good reference to the Items
collection should do it ...
>> //Step #1 bind the data:
>> public bool ListAllowedSources(int xUserID,
[quoted text clipped - 83 lines]
>>>>
>>>> Thank you for your help!
xzzy - 24 Nov 2007 16:34 GMT
Thank you for you time looking into this. Here is the code-behind for the
web page:
//in code-behind of Webform.aspx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private void Page_UnLoad(object sender, System.EventArgs e)
{
//The foreach is called in "SetAll"
SetAll("Page_UnLoad");
}
//in code-behind of Webform.aspx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private void Page_Load(object sender, System.EventArgs e)
{
try
{
if ( !IsPostBack )
{
//ListAllowedSources is in a class and is called from "RefreshAll"
RefreshAll();
}
}
catch (Exception ex)
{
}
finally
{
}
}
//in code-behind of Webform.aspx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private void SetAll(string xCalledFrom)
{
try
{
if ( ValidScreen() )
{
foreach(ListItem li in ctlLB.Items)
{
if ( li.Selected == true )
{
}
}
//in code-behind of Webform.aspx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private void RefreshAll()
{
try
{
clsSources mySource = new clsSource();
mySource.ListAllowedSources(yUserID, ctlTypes);
// Previous posts #########################################################
>> why: foreach (ListItem li in ctlLB.Items) when the ListBox is xListbox?
>>
[quoted text clipped - 102 lines]
>>>>>
>>>>> Thank you for your help!
xzzy - 24 Nov 2007 17:02 GMT
the error:
? ctlLB.Items
{System.Web.UI.WebControls.ListItemCollection}
System.Object: {System.Web.UI.WebControls.ListItemCollection}
Capacity: 16
Count: 0
IsReadOnly: false
IsSynchronized: false
//the next 2 lines are the underlying problem
Item: <cannot view indexed property>
listItems: {Count=0}
//
marked: true
saveAll: false
SyncRoot: {System.Web.UI.WebControls.ListItemCollection}
> Thank you for you time looking into this. Here is the code-behind for the
> web page:
[quoted text clipped - 162 lines]
>>>>>>
>>>>>> Thank you for your help!
xzzy - 24 Nov 2007 17:03 GMT
SetAll is also called whenever the viewer selects one of these command
buttons:
protected void DeleteBtn_Click(Object Sender, EventArgs e)
{
int NbrErrors = 0;
try
{
if ( !SetAll("DeleteBtn_Click") )
{
++NbrErrors;
}
}
catch (Exception ex)
{
++NbrErrors;
}
finally
{
if ( NbrErrors != 0 )
{
Response.Redirect(clsStaticVARS.WebSiteNameBrowser +
"ErrorPage.aspx",true);
}
}
}
protected void SubmitBtn_Click(Object Sender, EventArgs e)
{
int NbrErrors = 0;
try
{
if ( !SetAll("SubmitBtn_Click") )
{
++NbrErrors;
}
}
catch (Exception ex)
{
++NbrErrors;
}
finally
{
if ( NbrErrors == 0 )
> the error:
> ? ctlLB.Items
[quoted text clipped - 181 lines]
>>>>>>>
>>>>>>> Thank you for your help!
Liz - 24 Nov 2007 19:44 GMT
xzzy:
my gut tells me this is all WAY too much code to populate a listbox and
update a DB column with its value; this is basically easy stuff .. wht
don't you have a look at some code samples from other folks for ideas ..
OTOH, it would probably be worthwhile to understand why you can't iterate
the items collection ...
> SetAll is also called whenever the viewer selects one of these command
> buttons:
[quoted text clipped - 226 lines]
>>>>>>>>
>>>>>>>> Thank you for your help!