i am making an application in win forms in which there is a datagridview on the right and flields on the left when i enter some data gridview is not showing recently added record in a table
My Code is as follows
CRUD Class Code
{ static string ConString = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString; DataTable dt = new DataTable(); SqlConnection con = new SqlConnection(ConString); SqlCommand cmd = new SqlCommand(); #region----------> Select record public DataTable SelectRec() { string query = "select * from Users"; //Users user = new Users(); try { con.Open(); cmd.Connection = con; cmd.CommandText = query; SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { con.Close(); } return dt; } #endregion #region----------> Insert record public bool InsertRec(User u) { bool isOk = false; string query = "insert into Users(FirstName,LastName,Email,UserName,Password,Address,Contact,Gender,UserType,AddedDate,AddedBy)values(@FirstName,@LastName,@Email,@UserName,@Password,@Address,@Contact,@Gender,@UserType,@AddedDate,@AddedBy)"; try { cmd.Connection = con; cmd.CommandText = query; cmd.Parameters.AddWithValue("@FirstName",u.FirstName); cmd.Parameters.AddWithValue("@LastName",u.LastName); cmd.Parameters.AddWithValue("@Email",u.Email); cmd.Parameters.AddWithValue("@UserName",u.UserName); cmd.Parameters.AddWithValue("@Password",u.Password); cmd.Parameters.AddWithValue("@Address",u.Address); cmd.Parameters.AddWithValue("@Contact",u.Contact); cmd.Parameters.AddWithValue("@Gender", u.Gender); cmd.Parameters.AddWithValue("@UserType",u.UserType); cmd.Parameters.AddWithValue("@AddedDate",u.AddedDate); cmd.Parameters.AddWithValue("@AddedBy",u.AddedBy); con.Open(); //SqlDataAdapter sda = new SqlDataAdapter(cmd); //sda.Fill(dt); int run = cmd.ExecuteNonQuery(); if (run > 0) { isOk = true; } else { isOk = false; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { con.Close(); } return isOk; } #endregion #region----------> Update record public bool UpdateRec(User u) { bool isOk = false; string query = "update Users set FirstName=@FirstName,LastName=@LastName,Email=@Email,UserName=@UserName,Password=@Password,Address=@Address,Contact=@Contact,Gender=@Gender,UserType=@UserType,AddedDate=@AddedDate,AddedBy=@AddedBy where Id=@Id"; cmd.Connection = con; cmd.CommandText = query; try { cmd.Parameters.AddWithValue("@Id", u.Id); cmd.Parameters.AddWithValue("@FirstName", u.FirstName); cmd.Parameters.AddWithValue("@LastName", u.LastName); cmd.Parameters.AddWithValue("@Email", u.Email); cmd.Parameters.AddWithValue("@UserName", u.UserName); cmd.Parameters.AddWithValue("@Password", u.Password); cmd.Parameters.AddWithValue("@Address", u.Address); cmd.Parameters.AddWithValue("@Contact", u.Contact); cmd.Parameters.AddWithValue("@Gender", u.Gender); cmd.Parameters.AddWithValue("@UserType", u.UserType); cmd.Parameters.AddWithValue("@AddedDate", u.AddedDate); cmd.Parameters.AddWithValue("@AddedBy", u.AddedBy); con.Open(); //SqlDataAdapter sda = new SqlDataAdapter(cmd); //sda.Fill(dt); int run = cmd.ExecuteNonQuery(); if (run > 0) { isOk = true; } else { isOk = false; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { con.Close(); } return isOk; } #endregion #region----------> Delete record public bool DeleteRec(User u) { bool isOk = false; string query = "delete from Users where Id=@Id"; cmd.Connection = con; cmd.CommandText = query; cmd.Parameters.AddWithValue("@Id", u.Id); try { con.Open(); int run = cmd.ExecuteNonQuery(); if (run > 0) { isOk = true; } else { isOk = false; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { con.Close(); } return isOk; } #endregion #region----------> other #endregion }
On page load
private void Users_Load(object sender, EventArgs e) { dataGridView1.DataSource = udl.SelectRec(); }
Insert Snippet code - could also consider for update record just
private void button2_Click(object sender, EventArgs e) { int id = Convert.ToInt32(txtuserid.Text); u.Id = id; u.FirstName = txtfn.Text; u.LastName = txtln.Text; u.Email = txtEmail.Text; u.UserName = txtun.Text; u.Password = txtpassword.Text; u.Address = txtaddress.Text; u.Contact = txtcontact.Text; u.Gender = cbg.Text; u.UserType = cbut.Text; u.AddedDate = DateTime.Now; // u.AddedBy = u.Id; bool success = udl.UpdateRec(u); if (success) { MessageBox.Show("Record Successfully Updated"); //dataGridView1.Visible = false; } else { MessageBox.Show("Error occur during record update"); } Clear(); //dataGridView1.DataSource = udl.SelectRec(); //dataGridView1.Update(); //dataGridView1.Refresh(); //dataGridView1.Visible = true; }
Clear is a method to clear textbox values.
Help me out plaese a related tutorial would be appreciable