Click here to Skip to main content
15,939,065 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
hi , i wrote this code snippet, but i have not any result.
after compile, i have not any data in the ListBox.
Table in the database also have data.
please help me thank you.
public partial class _Default : System.Web.UI.Page 
    private string connectinString=
    protected void Page_Load(object sender, EventArgs e)
      private void FillAuthorList()
         string SelectSQL="SELECT id, au_lname, au_fname FROM Authors";
          SqlConnection con = new SqlConnection(connectinString);
          SqlCommand command = new SqlCommand(SelectSQL, con);
          SqlDataReader reader;
              reader = command.ExecuteReader();
              while (reader.Read())
                  ListItem newItem=new ListItem();
          catch(Exception Err)
              lblResult.Text="Error reading list";


[Edit]Code block added[/Edit]
Updated 26-Dec-12 0:42am
maysam_p82 26-Dec-12 6:43am    
Compile pointer goes to while loop and then jump to the con.close() method. without error.
Deenuji 26-Dec-12 6:48am    
pls post ur error below this code.....

1 solution

First off, if you only have one record in your DB table, then you will get nothing - because you discard the first record you read:
while (reader.Read())
Every time you call the Read method, the current record is replaced with the next. Get rid of the first line of these two to leave just the while loop.

Secondly, I would also recommend explicit casting:
newItem.Text=(string) reader["au_lname"] + "," + (string) reader["au_fname"];

You don't need it (as implicit ToString calls will probably be made), but it is worth doing from a readability and type safety point of view.

If you have more than one row of data, then you need to look at your lblResult label - do you get any message? That should help.
Share this answer
maysam_p82 26-Dec-12 6:58am    
i have more record stored in the table.
I don't have any message at the lblResult.
also,I said compiler does not go into the while loop
OriginalGriff 26-Dec-12 7:25am you didn't...
maysam_p82 26-Dec-12 7:03am    
I think dataReader couldn't find any data at the table.also i must say that i cant attach database in sqlserverexpress,Im using server explorer as good.
but im not sure every thing is okay about this kind of use.
OriginalGriff 26-Dec-12 7:28am    
If you aren't getting a message on your label, then what is happening? There are a number of ways this could go wrong, starting with your connection string - which if you are having trouble may be the source of your problem. Look at the string: edit your code to display the actual string you are using, and check it against the SQL server instance you are using.
Then check that it connects ok in your code, before moving to actual data.
I would suspect that you are trying to access the wrong DB - but that should give you an error message in the label.
maysam_p82 26-Dec-12 7:44am    
<add name="Pubs" connectionstring="data source=localhost\SQLEXPRESS;initial catalog=Pubs;integrated security=SSPI">
this is my string placed in the web.config
Also I tested another application that give the server version,and that worked successfully. I don't know why it doesn't work properly:(.
please help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900