Over a million developers have joined DZone.

Displaying SQL Data in a Text Box Using C# [Code Snippet]

Here's a quick and easy way to put SQL database info into a text box when using C#.

· Database Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RetrieveData {
    public partial class Form1 : Form
        public Form1()
        private void button1_Click(object sender, EventArgs e)
            bool temp = false;
            SqlConnection con = new SqlConnection("server=arun\\SQLEXPRESS;database=pan;Trusted_Connection=True");
            SqlCommand cmd = new SqlCommand("select * from Table2 where first='" + textBox1.Text.Trim() + "'", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
                textBox2.Text = dr.GetString(1);
                textBox3.Text = dr.GetString(2);
                temp = true;
            if (temp == false)
            MessageBox.Show("not found");

Related Refcard:

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

c# developer,sql server 2008

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}