473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Listbox update issue

I have a listbox that gets populated in code by a reader result set
from a stored proc on SQL Server 2005. In building this I created a
set of test rows in the DB and the code runs fine and populates the
box. Next I added functionality to add data. This adds data to the DB
and then forces the listbox to refresh by clearing it's contents and
then re-running the populate code.

However, when I add an item and do the refresh, the listbox does not
get the new entry. I debugged this and the DB gets updated (in fact I
stopped just after the DB update code and ran the proc that gets the
listbox data on the SQL server manager and it showed that the data was
indeed there.

But once the list box populate code runs, the new entry is missing. If
I terminate the application and run it again, the new data shows up.
Is something getting cached with the SQL reader object? It falls out
of scope in the proc that populates the list box so I assumed that it
was gone and got re-created on the next call. Both the reader object
and the connection object have their .close methods called before the
populate proc ends.

Can someone shed some light?

Thanks!

Sep 4 '06 #1
5 2276
heddy,

Can you show how you are populating the listbox, as well as performing
the update?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"heddy" <he*******@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
>I have a listbox that gets populated in code by a reader result set
from a stored proc on SQL Server 2005. In building this I created a
set of test rows in the DB and the code runs fine and populates the
box. Next I added functionality to add data. This adds data to the DB
and then forces the listbox to refresh by clearing it's contents and
then re-running the populate code.

However, when I add an item and do the refresh, the listbox does not
get the new entry. I debugged this and the DB gets updated (in fact I
stopped just after the DB update code and ran the proc that gets the
listbox data on the SQL server manager and it showed that the data was
indeed there.

But once the list box populate code runs, the new entry is missing. If
I terminate the application and run it again, the new data shows up.
Is something getting cached with the SQL reader object? It falls out
of scope in the proc that populates the list box so I assumed that it
was gone and got re-created on the next call. Both the reader object
and the connection object have their .close methods called before the
populate proc ends.

Can someone shed some light?

Thanks!

Sep 4 '06 #2

Nicholas Paldino [.NET/C# MVP] wrote:
heddy,

Can you show how you are populating the listbox, as well as performing
the update?

The code that does the update of the listbox looks like this:

bool bResult = true; // assume success
SqlConnection Conn;
SqlDataReader reader = null;
this.lstProjects.Items.Clear();
Conn = new SqlConnection("user id=" + this.oUser.sUID + ";"
+
"password=" + this.oUser.sPW + ";"
+
this.oUser.sConnectString);
try
{
Conn.Open();
}

catch (Exception except)
{
bResult = false;
MessageBox.Show("Database connection Failed for user: "
+ this.oUser.sUID+ " -- Issue reported was " + except.Message);
return (bResult);
}

SqlCommand cmd = new SqlCommand("spGetProjectList",
Conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UID",
this.oUser.sUID));
reader = cmd.ExecuteReader();

if (reader.HasRows)
{
// We have projects - Populate the list
while(reader.Read())
{

this.lstProjects.Items.Add(reader.GetSqlString(0)) ;

}

this.lstProjects.SelectedIndex=0;

}

else
{
this.lstProjects.Items.Add("No Projects Defined for
this user");
}
}

reader.Close();
Conn.Close();

return (bResult);

This code actually resides within a try/catch block but that code is
irrelevant for this (I think :) ).

Anyway, that code populates the list box as you can see. I even tried
reader.Dispose() instead of close in case it was caching something.

Thanks for looking :)

Sep 4 '06 #3

Nicholas Paldino [.NET/C# MVP] wrote:
heddy,

Can you show how you are populating the listbox, as well as performing
the update?
Oh - and the DB update code here:

bool CreateNewProject()
{
SqlConnection Conn;
//IAsyncResult iaResult = null;
int iRows = 0;

SqlDataReader reader = null;

Conn = new SqlConnection("user id=" +
this.oUser.sUID.ToString() + ";" +
"password=" +
this.oUser.sPW.ToString() + ";" +
this.oUser.sConnectString);
try
{
Conn.Open();
}

catch (Exception except)
{
MessageBox.Show("Login Failed for user: " +
this.oUser.sUID.ToString() + " -- Issue reported was " +
except.Message);
}

try
{
SqlCommand cmd = new SqlCommand("spGetAuthority",
Conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UserName",
this.oUser.sUID.ToString()));
try
{
reader = cmd.ExecuteReader();
}
catch (Exception except)
{

MessageBox.Show("Database failure -- Issue reported
was " + except.Message);
return(false);

}
if (reader.HasRows)
{
cmd = new SqlCommand("spCreateProject", Conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UserName",
this.oUser.sUID.ToString()));
cmd.Parameters.Add(new SqlParameter("@ProjectName",
this.txtProjectName.Text));
cmd.Parameters.Add(new SqlParameter("@ProjectPath",
this.txtRootPath.Text));
cmd.Parameters.Add(new
SqlParameter("@ProjectComments", this.txtComments.Text));
try
{
iRows = cmd.ExecuteNonQuery();
}
catch (Exception except)
{
MessageBox.Show("Database failure -- Issue
reported was " + except.Message);
return(false);
}

if (iRows==0)
{
MessageBox.Show("The database update failed - 0
rows were added");
return (false);
}

}

else
{
MessageBox.Show("User Database connect Login
Failed");
return (false);
}
}

finally
{
reader.Close();
Conn.Close();
}
return (true);
}

Sep 4 '06 #4
Oh - on the update DB code, after the line:

if (reader.HasRows)
{

I do a reader.close(); - For some reason that did not come across in my
paste (*boggle*).

Sep 4 '06 #5
Turns out the code ran asynch with the form so the update happened as
soon as the form opened and never happened when the form closed - so I
just opened the form as modal dialog box and it's all good!

Sep 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: N. Graves | last post by:
Hello... thank you for your time. I have a form that has a List box of equipotent records and a sub form that will show the data of the equipment select from the list box. Is it possible to...
4
by: Alienz | last post by:
I have a subform where I have a subform with 20 options to select from. When I set the multiselect property to simple and select multiple options, nothing is stored. I have another table with...
6
by: AAJ | last post by:
Hi all I have a listbox on a form. If I set its rowsource directly, and the query in the rowsourse returns no data, then the displayed listbox is empty (exactly as you would expect) ...
3
by: google | last post by:
Hello, I am trying to create a listbox that users can select multiple entries on. I want Access to put each on of those selections in a different row on a particular table. This table will also...
1
by: NBB | last post by:
I can't figure this one out. Here's the situation, should be pretty for the pros in here. I have a ListBox that is populated with the DataTextField and DataValueField flags of the DataSet....
4
by: lgbjr | last post by:
Hi All, I've got a listbox on a VB.NET form. when the form opens, the ListBox SelectionMode is set to Single. while running various routines on the form, items get added to the list box (results...
6
by: Mark | last post by:
Hello. I have a listbox whose rowsource is set to a saved query (call it "qry_customer_list.") When I add customers to my database, I call the listbox Requery method so that the listbox will...
2
by: Steve Potter | last post by:
I am trying to find some method of attaching a Listbox object to a list object so as the contents of the list are changed the contents of the Listbox will be updated to match. I have found a few...
3
by: deejayquai | last post by:
Hello Simple one this I guess, but I'm quite stuck at the moment. I would like to update the records displayed in my listbox (lstStudents) using criteria selected from my combo (cboForm) in a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.