Change VB to C# code and How to set array

G

Guest

Hi,

I read http://www.dotnetjohn.com/articles/articleid57.aspx and I need the C# version.

Here is the VB code
Sub ChangeWhereClause(Sender As System.Object, e As System.EventArgs)
Dim strWhereClause As String = ""
For Each li in lsbEmployee.Items
If li.Selected Then
strWhereClause &= "EmployeeID=" & li.Value & " Or "
End If
Next
If strWhereClause.Length > 0 Then
dtgEmployee.Visible = True
'Chop off last " Or "
strWhereClause = Left(strWhereClause, strWhereClause.Length() - 4)
strWhereClause = "WHERE " & strWhereClause

1. How can I convert the foreach loop? I tried foreach(li in lsbEmployee.Items), but "in" has a red underline to show error in cs file.
2. How can I convert the 'Chop off last " Or " ?
3. How can I convert the Left(strWhereClause, strWhereClause.Length() - 4) ?

Array question:

I have a string variable

protected string SelectedDeptIDs
{
get
{
return ViewState["SelectedDeptIDs"] == null? "":(string)ViewState["SelectedDeptIDs"];
}
set
{
ViewState["SelectedDeptIDs"] = value;
}
}

private void InitializeComponent()
{
this.listDept.SelectedIndexChanged += new System.EventHandler(this.listDept_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void listDept_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.SelectedDeptIDs = this.listDept.SelectedItem.Value;
ListStaffDataBind();
}

Since there are many this.listDept.SelectedItem.Value in a listbox, how can I save their values to SelectedDeptIDs? Should I use array?

After setting it, how can I retrieve the individual SelectedDeptIDs? How can I split them?

Thanks
 
A

Andreas Håkansson

Tom,

(1) foreach(string li in lsbEmployee.Items)
(2) This is a VB.NET comment and not code that is
executed by the application.
(3) strWhereClause.Substring(0, strWhereClause.Lenght - 4);

Since it's close to 4am here, I will leave the ViewState question for
someone else to help you with.

Hope this helps,

//Andreas

Tom said:
Hi,

I read http://www.dotnetjohn.com/articles/articleid57.aspx and I need the C# version.

Here is the VB code
Sub ChangeWhereClause(Sender As System.Object, e As System.EventArgs)
Dim strWhereClause As String = ""
For Each li in lsbEmployee.Items
If li.Selected Then
strWhereClause &= "EmployeeID=" & li.Value & " Or "
End If
Next
If strWhereClause.Length > 0 Then
dtgEmployee.Visible = True
'Chop off last " Or "
strWhereClause = Left(strWhereClause, strWhereClause.Length() - 4)
strWhereClause = "WHERE " & strWhereClause

1. How can I convert the foreach loop? I tried foreach(li in
lsbEmployee.Items), but "in" has a red underline to show error in cs file.
2. How can I convert the 'Chop off last " Or " ?
3. How can I convert the Left(strWhereClause, strWhereClause.Length() - 4) ?

Array question:

I have a string variable

protected string SelectedDeptIDs
{
get
{
return ViewState["SelectedDeptIDs"] == null? "":(string)ViewState["SelectedDeptIDs"];
}
set
{
ViewState["SelectedDeptIDs"] = value;
}
}

private void InitializeComponent()
{
this.listDept.SelectedIndexChanged += new System.EventHandler(this.listDept_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void listDept_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.SelectedDeptIDs = this.listDept.SelectedItem.Value;
ListStaffDataBind();
}

Since there are many this.listDept.SelectedItem.Value in a listbox, how
can I save their values to SelectedDeptIDs? Should I use array?
 
M

Morten Wennevik

Hi Tom,

4.

A listbox has a SelectedItems (plural) property containing a list of
selected objects
(By the way, there is no SelectedItem.Value, you may be thinking of
SelectedValue)


Assuming SelectedDeptIDs is an ArrayList
foreach(object o in this.listDept.SelectedItems)
{
this.SelectedDeptIDs.Add(o);
}

Assuming the listbox contains a list of integers
Assuming SelectedDeptIDs is an int array
this.SelectedDeptIDs = new int[this.listDept.SelectedItems.Count];
for(int i = 0; i < this.SelectedDeptIDs.Length; i++)
{
this.SelectedDeptIDs = (int)this.ListDept.SelectedItems;
}

Happy coding!
Morten
 
R

Ravichandran J.V.

foreach(li in lsbEmployee.Items)

In C# you need to specify the type of the first parameter.

Rest of your problem after I understand them.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
G

Guest

Thank you for all of your kind help. The listbox works now.
All listitems can display in 2nd listbox when I select those in 1st listbox

I have one last problem in listbox.

In fact, for a set of 1st and 2nd listbox, it works fine now.
If I add one set of 3rd and 4th listbox, it has a problem

The listitems in 2nd listbox display. But, they will be removed automatically when I select listitems in 3rd listbox. The listbox refreshes the load_page and cannot store the selected value of 1st and 2nd listbox

How can I fix it?
 

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