This is my code for the child form which displays all the registered user details with edit buttons
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;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
DataSet set = GetRegisteredUsers();
dataGridView1.DataSource = set.Tables[0];
DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
EditColumn.Text = "Edit";
EditColumn.Name = "Edit";
EditColumn.DataPropertyName = "Edit";
dataGridView1.Columns.Add(EditColumn);
EditColumn.UseColumnTextForButtonValue = true;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//RegisterUser frm = new RegisterUser();
//frm.Show();
Profile childForm = new Profile();
childForm.MdiParent = (Form)this.Parent.Parent;
//childForm.Text = "Window ";
childForm.Show();
}
public DataSet GetRegisteredUsers()
{
try
{
//local variables
DataSet ds = new DataSet();
//creating conection object
SqlConnection con = new SqlConnection();
// connection string
con.ConnectionString = @"Data Source=AGILE\SBSMONITORING;Initial Catalog=Training_Alekhya;Persist Security Info=True;User ID=sa;Password=agile_sa@2011";
SqlCommand cmd = new SqlCommand();
{
//set up command object
SqlDataAdapter da = new SqlDataAdapter("(select * from Registration)", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
//opening connection
if (con.State != ConnectionState.Open)
{
con.Open();
}
//executing the query and filling the data set
da.Fill(ds);
//close connection
con.Close();
//returning the result of stored procedure
return ds;
}
}
catch (Exception ex)
{
MessageBox.Show("exception caught:" + ex.ToString() + ".", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
}
}
and this is my code for update page
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;
namespace WindowsFormsApplication2
{
public partial class Profile : Form
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader rdr;
public Profile()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string gender = "";
if (radioButton1.Checked == true)
{
gender = "M";
}
else if (radioButton2.Checked == true)
{
gender = "F";
}
// creating conection object
SqlConnection con = new SqlConnection();
//creating a conection string
con.ConnectionString = @"Data Source=AGILE\SBSMONITORING;Initial Catalog=Training_Alekhya;Persist Security Info=True;User ID=sa;Password=agile_sa@2011";
//SqlDataAdapter adp = new SqlDataAdapter("insert into Registration values( @UserName,@Password,@ConfirmPassword,@Phone,@Email,@Address,@Gender,@AccessRights)", con);
//SqlCommand cmd = new SqlCommand();
con.Open();
SqlCommand cmd = new SqlCommand("Update_table", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UserName", textBox1.Text));
cmd.Parameters.Add(new SqlParameter("@Password", textBox2.Text));
cmd.Parameters.Add(new SqlParameter("@Gender", gender));
cmd.Parameters.Add(new SqlParameter("@Email", textBox5.Text));
cmd.Parameters.Add(new SqlParameter("@Phone", textBox4.Text));
cmd.Parameters.Add(new SqlParameter("@Address", textBox6.Text));
cmd.Parameters.Add(new SqlParameter("@AccessRights", comboBox1.SelectedItem));
cmd.Parameters.Add(new SqlParameter("@userid", 1));
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(("select * from Registration where userid = @userid"), con);
int nr = cmd.ExecuteNonQuery();
if (nr > 0)
{
this.Hide();
Form1 lgn = new Form1();
lgn.ShowDialog();
MessageBox.Show("Saved");
}
else
MessageBox.Show("Not Saved");
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
// {
// string Gender = "";
// if (radioButton1.Checked == true)
// {
// Gender = "m";
// }
// else if (radioButton2.Checked == true)
// {
// Gender = "f";
// }
// //creating conection object
// //SqlConnection con = new SqlConnection();
// //creating a conection string
// //con.ConnectionString = @"Data Source=AGILE\SBSMONITORING;Initial Catalog=Training_Alekhya;Persist Security Info=True;User ID=sa;Password=agile_sa@2011";
// //SqlDataAdapter adp = new SqlDataAdapter("insert into Registration values( @UserName,@Password,@ConfirmPassword,@Phone,@Email,@Address,@Gender,@AccessRights)", con);
// //SqlCommand cmd = new SqlCommand();
// //cmd.Connection = con;
// //changing conection string to stored procedures
// cmd.CommandType = CommandType.StoredProcedure;
// cmd.CommandText = "dbo.Update_table";
// //"insert into Registration values( @UserName,@Password,@ConfirmPassword,@Phone,@Email,@Address,@Gender,@AccessRights)";
// cmd.Parameters.AddWithValue("@UserName", textBox1.Text);
// cmd.Parameters.AddWithValue("@Password", textBox2.Text);
// //cmd.Parameters.AddWithValue("@ConfirmPassword", textBox3.Text);
// cmd.Parameters.AddWithValue("@Phone", textBox4.Text);
// cmd.Parameters.AddWithValue("@Email", textBox5.Text);
// cmd.Parameters.AddWithValue("@Address", textBox6.Text);
// cmd.Parameters.AddWithValue("@Gender", Gender);
// //cmd.Parameters.AddWithValue("@Gender", radioButton2.Checked);
// cmd.Parameters.AddWithValue("@AccessRights", comboBox1.SelectedItem);
// //textBox1.Text=(("Select UserName from Registration where UserId = @UserId"), con);
// // try
// // {
// // if (textBox2.Text == textBox3.Text)
// // {
// // con.Open();//establishes phy link between App and DB
// cmd.ExecuteNonQuery();
// // MessageBox.Show("You have been registered");
// // this.Hide();
// // Form1 lgn = new Form1();
// // lgn.ShowDialog();
// // }
// // else
// // {
// // MessageBox.Show("does not match");
// // textBox3.Clear();
// // textBox3.Focus();
// // }
// // }
// // catch (SqlException ex)
// // {
// // MessageBox.Show(ex.Message);
// // }
// // finally
// // {
// if (con.State == ConnectionState.Open)
// {
// con.Close();
// }
// // }
// }
private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) == true)
{
if (textBox4.Text.Length < 10)
{
//MessageBox.Show("invalid mobile no");
}
else
{
MessageBox.Show("Max 10 chars");
e.Handled = true;
}
}
else
{
e.Handled = true;
}
}
private void Profile_Load(object sender, EventArgs e)
{
//Form.DataSource = bindingSource1;
//GetData("select * from Customers");
con = new SqlConnection("Data Source=192.168.1.2;database=Training_Reshma;Persist Security Info=True;User ID=sa;Password=agile_sa@2011");
con.Open();
cmd = new SqlCommand("Select * from tblregistration", con);
rdr = cmd.ExecuteReader();
con.Close();
}
}
}