Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

problem in Database Connection and rendering.

Posted on 2004-10-26
12
Medium Priority
?
288 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:heyday2004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12415362
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
 

Author Comment

by:heyday2004
ID: 12415593
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12415734
heyday2004,

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

Regards!
B..M
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:heyday2004
ID: 12415835
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12415968
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
 

Author Comment

by:heyday2004
ID: 12416886
thanks. but nothing happens, no exception, no error, just couldn't post text to the textbox.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12416973
heyday2004,

Unfortunatelly i can not think of anything else
sorry

Regards!
B..M
0
 
LVL 4

Assisted Solution

by:vinhthuy_nguyen
vinhthuy_nguyen earned 100 total points
ID: 12418056
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
 

Author Comment

by:heyday2004
ID: 12418354
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
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
ID: 12418631
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
 

Author Comment

by:heyday2004
ID: 12418774
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12418806
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question