Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how can I get data from database and display in textbox

how can I get data from database and display in textbox

Scheduled Pinned Locked Moved C#
databasehelpcsharpsql-serversysadmin
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adekolurejo
    wrote on last edited by
    #1

    I want the data stored in database to be displayed from the first record in the database. These data were submited to the database via textbox and I want to load the first record from database and display in the textbox. below are the codes that I have check Beginners guide to accessing SQL Server through C# but can't still do it. No error but dont know what to do. Please I need help on this Thanks Ademola. private void COMPANY_INFO_Load(object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection("user id=sa;" + "password=admin123;server=ADEMOLAPC;" + "Trusted_Connection=yes;" + "database=Kay_Nylon_Db; " + "connection timeout=30"); myConnection.Open(); //try //{ SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select * from Company_Info", myConnection); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Console.WriteLine(myReader["Company_Id"].ToString()); Console.WriteLine(myReader["Company_Name"].ToString()); Console.WriteLine(myReader["Company_Address_Line1"].ToString()); Console.WriteLine(myReader["Company_Address_Line2"].ToString()); Console.WriteLine(myReader["Fax"].ToString()); Console.WriteLine(myReader["Email"].ToString()); }

    D J D I 4 Replies Last reply
    0
    • A Adekolurejo

      I want the data stored in database to be displayed from the first record in the database. These data were submited to the database via textbox and I want to load the first record from database and display in the textbox. below are the codes that I have check Beginners guide to accessing SQL Server through C# but can't still do it. No error but dont know what to do. Please I need help on this Thanks Ademola. private void COMPANY_INFO_Load(object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection("user id=sa;" + "password=admin123;server=ADEMOLAPC;" + "Trusted_Connection=yes;" + "database=Kay_Nylon_Db; " + "connection timeout=30"); myConnection.Open(); //try //{ SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select * from Company_Info", myConnection); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Console.WriteLine(myReader["Company_Id"].ToString()); Console.WriteLine(myReader["Company_Name"].ToString()); Console.WriteLine(myReader["Company_Address_Line1"].ToString()); Console.WriteLine(myReader["Company_Address_Line2"].ToString()); Console.WriteLine(myReader["Fax"].ToString()); Console.WriteLine(myReader["Email"].ToString()); }

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Your connection string is wrong to start with. If you supplied a username and password you do not use TrustedConnection. The rest of the code looks reasonable, so long as the SQL statement is correct and there's data in the Company_Info table, and the column names are correct.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Your connection string is wrong to start with. If you supplied a username and password you do not use TrustedConnection. The rest of the code looks reasonable, so long as the SQL statement is correct and there's data in the Company_Info table, and the column names are correct.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        A Offline
        A Offline
        Adekolurejo
        wrote on last edited by
        #3

        Thanks Dave, I have removed the trusted connection but can not still retrieve from the database. All I want is onformLoad it should get the record from database and display it in textbox. Expecting your reply Ademola.

        D 1 Reply Last reply
        0
        • A Adekolurejo

          I want the data stored in database to be displayed from the first record in the database. These data were submited to the database via textbox and I want to load the first record from database and display in the textbox. below are the codes that I have check Beginners guide to accessing SQL Server through C# but can't still do it. No error but dont know what to do. Please I need help on this Thanks Ademola. private void COMPANY_INFO_Load(object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection("user id=sa;" + "password=admin123;server=ADEMOLAPC;" + "Trusted_Connection=yes;" + "database=Kay_Nylon_Db; " + "connection timeout=30"); myConnection.Open(); //try //{ SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select * from Company_Info", myConnection); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Console.WriteLine(myReader["Company_Id"].ToString()); Console.WriteLine(myReader["Company_Name"].ToString()); Console.WriteLine(myReader["Company_Address_Line1"].ToString()); Console.WriteLine(myReader["Company_Address_Line2"].ToString()); Console.WriteLine(myReader["Fax"].ToString()); Console.WriteLine(myReader["Email"].ToString()); }

          J Offline
          J Offline
          Josh Martin
          wrote on last edited by
          #4

          When I am working with a datareader, I always try and make sure that I actually received a datareader back from the ExecuteReader() call (ie "if(myReader != null)". I also check the HasRows property on the datareader object to verify that I have results to loop through. The way this is coded now, the while loop will read each record from the datareader. If data from the first row is all that is needed, calling myReader.Read() by itself will just read the first row (instead of using the while loop).

          Josh Find a penny, pick it up, and all day long you'll have a back-ache...

          1 Reply Last reply
          0
          • A Adekolurejo

            Thanks Dave, I have removed the trusted connection but can not still retrieve from the database. All I want is onformLoad it should get the record from database and display it in textbox. Expecting your reply Ademola.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Don't "expect" anything from a site where you're not paying for support. OK. Do you have this code inside a Try/Catch block?? Remove the code from inside the Try block. There's too many people not putting anything in the catch block, silently eating any errors. If there are no errors, then the table is empty and there's nothing to return to you.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            1 Reply Last reply
            0
            • A Adekolurejo

              I want the data stored in database to be displayed from the first record in the database. These data were submited to the database via textbox and I want to load the first record from database and display in the textbox. below are the codes that I have check Beginners guide to accessing SQL Server through C# but can't still do it. No error but dont know what to do. Please I need help on this Thanks Ademola. private void COMPANY_INFO_Load(object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection("user id=sa;" + "password=admin123;server=ADEMOLAPC;" + "Trusted_Connection=yes;" + "database=Kay_Nylon_Db; " + "connection timeout=30"); myConnection.Open(); //try //{ SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select * from Company_Info", myConnection); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Console.WriteLine(myReader["Company_Id"].ToString()); Console.WriteLine(myReader["Company_Name"].ToString()); Console.WriteLine(myReader["Company_Address_Line1"].ToString()); Console.WriteLine(myReader["Company_Address_Line2"].ToString()); Console.WriteLine(myReader["Fax"].ToString()); Console.WriteLine(myReader["Email"].ToString()); }

              D Offline
              D Offline
              DavidKiryazi
              wrote on last edited by
              #6

              Ademola, can you post the latest code your using. It is difficult to diagnose the problem without the code you are currently trying to run. In the meantime, try to initialise your connection string using the connection string builder to rule out any typos in connection string.

              // create the connection string
              SqlConnectionStringBuilder conn_str = new SqlConnectionStringBuilder();
              conn_str.UserID = "your user_id";
              conn_str.Password = "your password";
              conn_str.InitialCatalog = "database name";
              conn_str.DataSource = "sql server instance name";
              conn_str.ConnectTimeout = "connection timeout (as an int)";

              // instantiate and initialise the sql connection object
              SqlConnection conn = new SqlConnection(conn_str.ConnectionString);

              Also, make sure you rule out any accidently brain explosions, ie make sure datatable actually has data in it, your connecting to the right database, etc Dave

              1 Reply Last reply
              0
              • A Adekolurejo

                I want the data stored in database to be displayed from the first record in the database. These data were submited to the database via textbox and I want to load the first record from database and display in the textbox. below are the codes that I have check Beginners guide to accessing SQL Server through C# but can't still do it. No error but dont know what to do. Please I need help on this Thanks Ademola. private void COMPANY_INFO_Load(object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection("user id=sa;" + "password=admin123;server=ADEMOLAPC;" + "Trusted_Connection=yes;" + "database=Kay_Nylon_Db; " + "connection timeout=30"); myConnection.Open(); //try //{ SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select * from Company_Info", myConnection); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Console.WriteLine(myReader["Company_Id"].ToString()); Console.WriteLine(myReader["Company_Name"].ToString()); Console.WriteLine(myReader["Company_Address_Line1"].ToString()); Console.WriteLine(myReader["Company_Address_Line2"].ToString()); Console.WriteLine(myReader["Fax"].ToString()); Console.WriteLine(myReader["Email"].ToString()); }

                I Offline
                I Offline
                ishandeb
                wrote on last edited by
                #7

                can i fetch null field from database while showing it in textbox on page load event..???

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups