In this tutorial I show you how link combo box with database value. here I use MySQL database load value. In previous tutorial I show how to connect with MySQL database. You can download all source code regarding this tutorial.
c# form query and code
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 MySql.Data.MySqlClient;
namespace ComboLord
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cmbLord();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void cmbLord()
{
try
{
string server = "localhost";
string database = "test";
string uid = "root";
string password = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
MySqlConnection con = new MySqlConnection(connectionString);
string sql = "SELECT `id` FROM `one`";
con.Open();
MySqlCommand sCommand = new MySqlCommand(sql, con);
MySqlDataReader reder = sCommand.ExecuteReader();
while (reder.Read())
{
cmblord.Items.Add(reder.GetString("id"));
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
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 MySql.Data.MySqlClient;
namespace ComboLord
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cmbLord();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void cmbLord()
{
try
{
string server = "localhost";
string database = "test";
string uid = "root";
string password = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
MySqlConnection con = new MySqlConnection(connectionString);
string sql = "SELECT `id` FROM `one`";
con.Open();
MySqlCommand sCommand = new MySqlCommand(sql, con);
MySqlDataReader reder = sCommand.ExecuteReader();
while (reder.Read())
{
cmblord.Items.Add(reder.GetString("id"));
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
No comments:
Post a Comment