Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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 ·
Free Resource

Find out how Database DevOps helps your team deliver value quicker while keeping your data safe and your organization compliant. Align DevOps for your applications with DevOps for your SQL Server databases to discover the advantages of true Database DevOps, brought to you in partnership with Redgate

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()
        {
            InitializeComponent();
            }
        private void button1_Click(object sender, EventArgs e)
        {
            bool temp = false;
            SqlConnection con = new SqlConnection("server=arun\\SQLEXPRESS;database=pan;Trusted_Connection=True");
            con.Open();
            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");
            con.Close();
            }
        }
    }

Related Refcard:

Align DevOps for your applications with DevOps for your SQL Server databases to increase speed of delivery and keep data safe. Discover true Database DevOps, brought to you in partnership with Redgate

Topics:
c# developer ,sql server 2008

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}