how can I get data from database and display in textbox
-
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 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()); }
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 -
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, 2008Thanks 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.
-
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()); }
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...
-
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.
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 -
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()); }
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
-
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()); }