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 Insert_button2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data source=.;initial catalog=names3;integrated security=true");
conn.Open();
SqlCommand cmd2 = new SqlCommand("Select username from table2 where username=@username", conn);
cmd2.Parameters.AddWithValue("username", textBox2.Text);
SqlDataReader myreader = cmd2.ExecuteReader();
if (myreader.Read())
{
conn.Close();
MessageBox.Show("Duplicate username");
}
else
{
conn.Close();
SqlCommand cmd = new SqlCommand("Insert into table2(name1,username)Values(@name1,@username)", conn);
cmd.Parameters.AddWithValue("name1", textBox1.Text);
cmd.Parameters.AddWithValue("username", textBox2.Text);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Data inserted successfully");
}
}
}
}
No comments:
Post a Comment