problem in Database Connection and rendering.

In my ASP.NET page, i met a problem in connection. Below is the simplified version of my program: It's a very simple and standard ASP.NET connection. What I need to do is to display the retrived data to the textbox: TextBox1.

I wrote this test line: TextBox1.Text= "test string" as below, but it doesn't work. When I move this line before the connection or after the closing of the connection, this line works and display "test string" in TextBox1 area. Anyone can tell me how to solve this problem (rendering text box with the retrived data while in connection). Thanks a million!

mySqlCommand.CommandText ="SELECT SHIFT_CODE FROM dbo.Emp_Mst WHERE BADGE_CD = '007727' ";
mySqlConnection.Open();
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
TextBox1.Text= "test string";  //test string not displayed
mySqlDataReader.Read();
mySqlDataReader.Close();
mySqlConnection.Close();
//TextBox1.Text= "test string";   no problem here then


thanks,
Scott
heyday2004Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmarinovCommented:
Hi heyday2004,

i assume that you read from the mySqlDataReader , not just adding the "test string" string to the TextBox1.Text
if i'm correct you have to move the TextBox1.Text = "test string" after the
mySqlDataReader.Read();

Regards!
B..M
0
heyday2004Author Commented:
Thanks for the answer. But seems it still doesn't work after i move the TextBox1.Text = "test string" after the
mySqlDataReader.Read(); It just simply could not display the simple string to textbox in the connection mode.
0
mmarinovCommented:
heyday2004,

it does not make sense
can you post the complete code, please ?

Regards!
B..M
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

heyday2004Author Commented:
Sure, here is the code. When user press button, then the textbox should show something. Other parts are the simplest, standard asp.net program so I don't list here. Thanks again.

private void Button_Click(object sender, System.EventArgs e)
            {
                  try
                  {
                        SqlConnection mySqlConnection =
                        new SqlConnection(
                        "Initial Catalog=RecordMaster; Data Source=ibm1;Integrated Security=SSPI");
                              
                        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
                              
                        mySqlCommand.CommandText =
                              "SELECT LAST_NAME" +
                              "FROM dbo.Emp_Mst " +
                              "WHERE BADGE_ID = '007727' ";
                                                
                        mySqlConnection.Open();
                        
                        SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
                        mySqlDataReader.Read();
                        TextBox1.Text= "test string";  //doesn't print anything
                        mySqlDataReader.Close();
                        mySqlConnection.Close(); //if put TextBox1.Text= "test string" here, then it is ok
                  }
                  catch (SqlException ex)
                  {
                        Console.WriteLine("A SqlException was thrown");
                        Console.WriteLine("Number = " + ex.Number);
                        Console.WriteLine("Message = " + ex.Message);
                        Console.WriteLine("StackTrace:\n" + ex.StackTrace);
                  }
            
            }
            
0
mmarinovCommented:
heyday2004,

can you debug and check if there is and exception? if you put the line when the connection is open - because the behaviour is the same

Regards!
B..M
0
heyday2004Author Commented:
thanks. but nothing happens, no exception, no error, just couldn't post text to the textbox.
0
mmarinovCommented:
heyday2004,

Unfortunatelly i can not think of anything else
sorry

Regards!
B..M
0
vinhthuy_nguyenCommented:
hi buddy,

There are something
1.     SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
2.     mySqlDataReader.Read();
3.     TextBox1.Text= "test string";  //doesn't print anything
4.     mySqlDataReader.Close();
5.      mySqlConnection.Close(); //if put TextBox1.Text= "test string" here, then it is ok

With 1. ' I think you should use MySQLCommand.ExecuteReader(CommandBehavior.CloseConnection) and you can delete the "4." line
        2. ' I don't know what're you gonna do with your datareader here ???

Nice day
0
heyday2004Author Commented:
Thanks for mmarinov  and vinhthuy's answers. I tried the suggested solution, but still had some problem.

This program is very simple, I just copied a data reader program from the book(which works in console window), and added the code to the Button_Click event handling. But seems when the connection is open, the form didn't accept postback or something. Below is a similar problem, I just added a similar test string, and although I defined it as string1="test string"; The compiler still said "Use of unsigned local variables: string1.

What's the problem? I think it's related with some very important concept such as variable scope or something but I couldn't figure out. Any further comment is really appreciated.


private void Button_Click(object sender, System.EventArgs e)
          {   string string1;   // newly added
               try
               {
                    SqlConnection mySqlConnection = new SqlConnection(
                    "Initial Catalog=RecordMaster; Data Source=ibm1;Integrated Security=SSPI");
                         
                    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
                         
                    mySqlCommand.CommandText =
                         "SELECT LAST_NAME" +
                         "FROM dbo.Emp_Mst " +
                         "WHERE BADGE_ID = '007727' ";
                                       
                    mySqlConnection.Open();
                    SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
                    mySqlDataReader.Read();

                    string1="test string";   //   newly added
                    mySqlDataReader.Close();
                    mySqlConnection.Close();
               }
               catch (SqlException ex)
               {
                    Console.WriteLine("A SqlException was thrown");
                    Console.WriteLine("Number = " + ex.Number);
                    Console.WriteLine("Message = " + ex.Message);
                    Console.WriteLine("StackTrace:\n" + ex.StackTrace);
               }
               TextBox1.Text= string1;   //newly added

          }
0
mmarinovCommented:
the new error is because the compiler does not know if the string1 has a value or not
to skip this error you have to modify this
string string1;   // newly added
with
string string1 = String.Empty;   // newly added


Regards!
B..M
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
heyday2004Author Commented:
Thanks, B..M! It eliminated the compiling error. But still, I couldn't print out string1 either during connection or after connection. Seems the connection is like a black hole. Nothing appeared when i tried to print out the value of the string1. Any idea? Thanks a lot again.
0
mmarinovCommented:
heyday2004,

i've tested you code it works great! so this means that the problem is related with your additional code
can you post the entire code behind ?

B..M
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.