Sakshi Kaul

Sakshi Kaul

  • NA
  • 72
  • 7.2k

Viewstate and postback handling

Apr 1 2019 2:29 AM
In the code below I want to handle postback and view state. Please help
and when I click on update button  it is not giving an error but its not updating the record. please help ?
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = @"Data Source=SAKSHIKAUL-LT\SQLEXORESS1;Initial Catalog=registrationForm;Integrated Security=True";
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{ SqlCommand cmd = new SqlCommand("insert into RegistrationTable" + "(EmpId,Nationality,Religion,Gender,Address,EmailId,Hobbies,MaritalStatus) values (@EmpId,@Nationality,@Religion,@Gender,@Address,@EmailId,@Hobbies,@MaritalStatus)", con);
//string insertQuery = "insert into RegistrationTable(EmpId,Nationality,Religion,Gender,Address,EmailId,Hobbies,Maritalstatus)values (@EmpId,@Nationality,@Religion,@Gender,@Address,@EmailId,@MHobbies,@Maritalstatus)";
cmd.Parameters.AddWithValue("@EmpId", TextBox1.Text);
cmd.Parameters.AddWithValue("@Nationality", TextBox2.Text);
cmd.Parameters.AddWithValue("@Religion", TextBox3.Text);
cmd.Parameters.AddWithValue("@Gender", TextBox4.Text);
cmd.Parameters.AddWithValue("@Address", TextBox5.Text);
cmd.Parameters.AddWithValue("@EmailId", TextBox6.Text);
cmd.Parameters.AddWithValue("@Hobbies", TextBox7.Text);
cmd.Parameters.AddWithValue("@Maritalstatus", TextBox8.Text);
cmd.ExecuteNonQuery();
Label1.Text = "Registered Successfuly";
}
protected void Button2_Click(object sender, EventArgs e)
{
cmd.CommandText = "update RegistrationTable set Nationality =' " + TextBox2.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set Religion =' " + TextBox3.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set Gender =' " + TextBox4.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set Address =' " + TextBox5.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set EmailId =' " + TextBox6.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set Hobbies =' " + TextBox7.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set MaritalStatus =' " + TextBox8.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.Connection = con;
cmd.ExecuteNonQuery();
Label1.Text = "Updated Successfuly";
}
protected void Button3_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = ""; //set equal to empty string to all fields
TextBox3.Text = ""; //set equal to empty string to all fields
TextBox4.Text = ""; //set equal to empty string to all fields
TextBox5.Text = ""; //set equal to empty string to all fields
TextBox6.Text = ""; //set equal to empty string to all fields
TextBox7.Text = ""; //set equal to empty string to all fields
TextBox8.Text = ""; //set equal to empty string to all fields
Label1.Text = "Clear All";
}
}

Answers (3)