C# - How To Search And Set Values From MySQL Database Into TextBox In C#
Source Code :
using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using MySql.Data.MySqlClient;namespace Csharp_And_MySQL
using System.Data;{ public partial class Csharp_MySQL_Search : Form { MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
MySqlCommand command;
MySqlDataReader mdr;
public Csharp_MySQL_Search() { InitializeComponent(); } private void BTN_SEARCH_Click(object sender, EventArgs e) { connection.Open();
string selectQuery = "SELECT * FROM test_db.users WHERE id=" + int.Parse(textBoxID.Text);
command = new MySqlCommand(selectQuery, connection);
mdr = command.ExecuteReader();
if(mdr.Read())
{
textBoxFName.Text = mdr.GetString("fname");
textBoxLName.Text = mdr.GetString("lname");
textBoxAge.Text = mdr.GetInt32("age").ToString();
}
else
{
textBoxID.Text = "";
textBoxLName.Text = "";
textBoxFName.Text = "";
textBoxAge.Text = "";
MessageBox.Show("No Data For This Id");
}
connection.Close();
} }}
Download Projects Source Code