Insertion Data In windows Form by Ado.Net For Begginers (Connecting & Inserting Data To DataBase<br />
By Using Try Catch Block & Sql Connection & Command Class Objects & Insert Command <br />
<br />Insertion Data In windows Form by Ado.Net For Begginers -Connecting & Inserting Data To DataBase By Using Try Catch Block Sql Connection & Command Class Objects & Insert Command
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;
using System.Data.Sql;
namespace AcceptButton
{
public partial class Registration : Form
{
//Oblects for sql class
SqlConnection cm;
SqlCommand cmd ;
public Registration()
{
//Connection String
cm = new SqlConnection();
cm.ConnectionString="Data Source=ONU-PC;Initial Catalog=AcceprtButton;User ID=sa; password=12345";
cmd=new SqlCommand();
cmd.Connection=cm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Object of form 1
Form1 fr = new Form1();
fr.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button9_Click(object sender, EventArgs e)
{
try
{
cmd.CommandText = "insert into Ab values ( @username, @password ,@email,@Name ,@Mobileno)";
cmd.Parameters.AddWithValue("@username", textBox1.Text);
cmd.Parameters.AddWithValue("@password", textBox2.Text);
cmd.Parameters.AddWithValue("@email", textBox4.Text);
cmd.Parameters.AddWithValue("@Name", textBox5.Text);
cmd.Parameters.AddWithValue("@Mobileno", textBox6.Text);
cm.Open();
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show("Failed");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Sry Exception");
}
finally
{
cm.Close();
}
}
private void Registration_Load(object sender, EventArgs e)
{
}
}
}
In this MemReg:-Button 9
& Exit:-Button2