I have a GridView and a submit button on the same page. Please each time i submit data to the database it does not reflect on the grid view immediately.
I have to refresh the page manage manually first.
Please help
What i have tried
public partial class Setting : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserDetails();
}
MainView.ActiveViewIndex = 0;
}
protected void BindUserDetails()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("Select id, category FROM AssetsCategory ORDER BY id ASC", con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
}
}
protected void Button4_Click(object sender, EventArgs e)
{
string insertSQL;
insertSQL = "INSERT INTO AssetsCategory (";
insertSQL += "Category)";
insertSQL += "VALUES('";
insertSQL += TextBox4.Text + "')";
String connectionString = "Data Source=NER\SQLEXPRESS01; Initial Catalog= kaging;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(insertSQL, con);
int added = 0;
try
{
con.Open();
added = cmd.ExecuteNonQuery();
if (added == 1)
{
MainView.ActiveViewIndex = 1;
}
else
{
Label5.Text = "Error @ unit 7";
}
}
catch (Exception err)
{
Response.Write(err.ToString());
}
finally
{
con.Close();
}
}
}