How to show data in textbox using C# .Net?

Here you will learn how to show data in textbox?. To show the data in textbox first you need to insert the data in database and then we will perform textbox operation, for that just follow the below Steps.

Step 1.

Create a New window form application in Microsoft Visual Studio C#.

new form

Step 2.

Now design your window form application like below form.

textbox

Step 3.

Open your Microsoft SQL Server and create a table like as below. My default database is master or you can create your own database if you like.

sql table

Step 4.

Now move to window form application(Form1.cs) and add following namespace in coding window. Simply double click on window form.

	
	using System.Data.SqlClient;
	
namespace

Step 5.

Now create a connection string to connect with Database. Make your Connection globally like below so that you can access it anywhere in the form.

	
	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.Data.SqlClient;

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

        	//globaly define conection
        	SqlConnection con = new SqlConnection("data source=DESKTOP-F3B3CCJ\SQLEXPRESS; database=master; uid=sa; password=123456;");
        	
    	}
	}
	

You will find your connection details in Sql Server. Open your Microsoft SQL Server.

Go to file menu → Connect Object Explorer….

connection

Step 6.

Now move to window form design view and double click on insert button. Add following code on your insert button click.


	    //insert button coding
        private void button1_Click(object sender, EventArgs e)
        {    
         try
            {        
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into student values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')", con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Data Inserted Successfully.");
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
		

Step 7. Final Step

Now double click on show button and write the below coding. For show the data in textbox, I used same textbox. I will show the record with unique identity, so I am using roll no. as unique identity. So in select command I am passing textbox4.

	
	//show button coding
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
            	SqlCommand cmd = new SqlCommand("select * from student where roll_no='" + textBox4.Text + "'", con);
            	SqlDataReader reader = cmd.ExecuteReader();
            	if(reader.Read())
            	{
                	textBox1.Text = reader[0].ToString();
                	textBox2.Text = reader[1].ToString();
                	textBox3.Text = reader[2].ToString();
            	}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
	

Output

Now run your application or simply press f5. Now insert the some records in database and then click on insert button.

Now enter the roll no. in textbox which record you want to show and then click on show button. You will get your record in same textbox. Here roll no. is unique identity in table, so we are fetching the records from database using roll no.

textbox

Watch Tutorial Video on YouTube

Watch and Subscribe Now

Post Your Comment