C# Using RadioButton With MySQL
How To Use A Radio Button With MySQL Database In C#
In This C# Tutorial We Will See How To Use A RadioButton Control With MySQL Database
- get value from the selected redio button and insert it into database .
- get value from mysql and select the specific radiobutton
In Csharp Programming Language And Visual Studio Editor.
- get value from the selected redio button and insert it into database .
- get value from mysql and select the specific radiobutton
In Csharp Programming Language And Visual Studio Editor.
PART 1
PART 2
Project Source 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 Csharp_Tutorials
{
public partial class RadioButton_And_MySQL : Form
{
public RadioButton_And_MySQL()
{
InitializeComponent();
}
// mysql connection
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='mydb';username=root;password=");
// button insert
private void buttonInsert_Click(object sender, EventArgs e)
{
MySqlCommand command = new MySqlCommand("INSERT INTO users(username,lang) VALUES (@usn,@lng)", connection);
command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = textBoxUsername.Text;
string rdbval = "";
// get the selected radio button value
if(radioButtonCSHARP.Checked)
{
rdbval = "C#";
}
else if (radioButtonVBNET.Checked)
{
rdbval = "VB.NET";
}
else if (radioButtonJAVA.Checked)
{
rdbval = "JAVA";
}
else
{
// the default value
rdbval = "NONE";
}
command.Parameters.Add("@lng", MySqlDbType.VarChar).Value = rdbval;
connection.Open();
// insert data into database
if(command.ExecuteNonQuery() == 1)
{
MessageBox.Show("Data Inserted");
}
else
{
MessageBox.Show("Data Not Inserted");
}
connection.Close();
// uncheck all radiobuttons
radioButtonCSHARP.Checked = false;
radioButtonVBNET.Checked = false;
radioButtonJAVA.Checked = false;
radioButtonNONE.Checked = false;
}
// button search
private void buttonSearch_Click(object sender, EventArgs e)
{
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM users WHERE id = " + textBoxID.Text,connection);
DataTable table = new DataTable();
adapter.Fill(table);
// table.Rows[0][0] = id
// table.Rows[0][1] = username
// table.Rows[0][2] = lang
textBoxUsername.Text = table.Rows[0][1].ToString();
// check the the specific radiobutton
switch(table.Rows[0][2].ToString())
{
case"C#":
radioButtonCSHARP.Checked = true;
break;
case "VB.NET":
radioButtonVBNET.Checked = true;
break;
case "JAVA":
radioButtonJAVA.Checked = true;
break;
case "NONE":
radioButtonNONE.Checked = true;
break;
}
}
}
}
///////////////OUTPUT:
Posting Komentar untuk "C# Using RadioButton With MySQL"