Solved

problem in Database Connection and rendering.

Posted on 2004-10-26
287 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
Question by:heyday2004
    12 Comments
     
    LVL 28

    Expert Comment

    by:mmarinov
    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
    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
    heyday2004,

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

    Regards!
    B..M
    0
     

    Author Comment

    by:heyday2004
    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
    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
    thanks. but nothing happens, no exception, no error, just couldn't post text to the textbox.
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    heyday2004,

    Unfortunatelly i can not think of anything else
    sorry

    Regards!
    B..M
    0
     
    LVL 4

    Assisted Solution

    by:vinhthuy_nguyen
    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
    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:
    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
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    Suggested Solutions

    Title # Comments Views Activity
    Ajax and MVC 2 41
    UPLOADING ASP.NET WEBSITE TO GODADDY 2 18
    Excel / ASP MVC 3 Website Intergration 2 26
    Asp.net mvc C# 2 21
    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    845 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

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now