Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
I am working on Visual Studio using C#. I need to get data from different database tables in SQL server into text boxes onto the Visual Studio form. The database tables have several rows and columns. The values are real values. I am using the code below. I don’t get the data to text boxes. Can anyone please help? Thanks.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


private void Form1_Load(object sender, EventArgs e)
        {
              SqlConnection con = new SqlConnection("Data Source=192.146.1.5,1433;Network Library=DBMSSOCN;Initial Catalog=Estimator;User ID=id;Password=password;Trusted_Connection=True");

                con.Open();

                SqlCommand cmd = new SqlCommand("select * from Table1", con);
                SqlDataReader dr = cmd.ExecuteReader();
                
            while (dr.Read())
            {
                textBox1.Text = dr.GetString(0);
                textBox2.Text = dr.GetString(1);
                textBox2.Text = dr.GetString(2);
                
            }
            con.Close();    
        }
     }
}
Posted
Updated 1-Jun-16 4:51am
v4
Comments
Herman<T>.Instance 1-Jun-16 8:16am    
Which excpetion?
GetString starts with 0 and not 1 for the first column. Are you aware of that?
Nagarjuna99 1-Jun-16 9:12am    
what values do you have for the textboxs?,
columns values? or rows values?
please explain briefly your question @savi kolla
Savi kolla 1-Jun-16 10:29am    
The database tables in the SQL server have several rows and columns. The values are real values. I need get some of them to the text boxes in Visual Studio. Thanks.

See Retrieving Data Using a DataReader[^] for the correct use of the SQLDataReader. As your items are real values, you should first extract them into real variables (i.e. Doubles). You can then convert them to text with something like String.Format() or variable.ToString(): see Double Structure (System)[^].
 
Share this answer
 
textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();
textBox3.Text = dr[2].ToString();
 
Share this answer
 
v2
Comments
Savi kolla 1-Jun-16 9:34am    
Thanks so much for your help. I still did not get any data when I changed the code to dr[0].ToString();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900