Item wont add to list box
Im converting this simple program from vb to c# it updates, displays, create and delete items from a little access database. Bellow is the PopulateListBox() function in VB it goes through every row in the data set and see if it is deleted, not deleted or has errors. I am getting 2 errors here both on the lines lstAlbums.Items.Add(item); and lstAlbums.Items.Add(delitem); now i realise that the strings are unassigned so i added string item = ""; string delitem = "";
then when i start the program it fills every second item in th list box with a blank row.
How do i overcome this situation? thank you in advance anyone how can help me.
private void PopulateListBox()
{
string item;
string delitem;
//clear the list box
lstAlbums.Items.Clear();
//access each row in the data set table
foreach (DataRow row in myDataSet.Tables["albums"].Rows)
{
//list the nondeleted rows
if (!((row.RowState & DataRowState.Deleted) == DataRowState.Deleted))
item = row["albumCode"] + ", " + row["AlbumTitle"] + ", " + row["ArtistCode"];
//list rows with update errors
if (row.HasErrors)
item = "(**" + row.RowError + "**)";
lstAlbums.Items.Add(item);
//list deleted rows
if ((row.RowState & DataRowState.Deleted) == DataRowState.Deleted)
delitem = row["albumCode", DataRowVersion.Original] + ", "
+ row["AlbumTitle", DataRowVersion.Original] + ", "
+开发者_开发知识库 row["ArtistCode", DataRowVersion.Original] + "***DELETED***";
lstAlbums.Items.Add(delitem);
}
In your code there is more then on line in if condition. I think thats creats a problem for you. So first include your code between {} which depends on if condition then Check it.
You're missing the opening and closing braces {
and }
in your if
statement, so the statement immediately following the if
statement executes if the condition matches, and the statement after that always executes.
In VB.NET your if
statement resembles this:
If condition Then
' do something
' do something else
End If
To translate that to C# and properly execute the 2 lines of code you should use braces:
if (condition)
{
// do something
// do something else
}
Your current translation actually looks like this:
if (condition)
// do something (if condition is true)
// do something else (not dependent on the condition)
In other words the 2nd statement always executes. It's a good practice to always enclose multiple statements within the braces to avoid such unintended circumstances.
Change your code to this:
if (!((row.RowState & DataRowState.Deleted) == DataRowState.Deleted))
{
item = row["albumCode"] + ", " + row["AlbumTitle"] + ", " + row["ArtistCode"];
//list rows with update errors
if (row.HasErrors)
{
item = "(**" + row.RowError + "**)";
}
lstAlbums.Items.Add(item);
}
//list deleted rows
if ((row.RowState & DataRowState.Deleted) == DataRowState.Deleted)
{
delitem = row["albumCode", DataRowVersion.Original] + ", "
+ row["AlbumTitle", DataRowVersion.Original] + ", "
+ row["ArtistCode", DataRowVersion.Original] + "***DELETED***";
lstAlbums.Items.Add(delitem);
}
精彩评论