I’m just starting with my Visual Studio 2010,wanted to create a website, I have 2 drop down lists and 4 textbox’s from which user enters the data and database should be updated but its not happening, grid view is just showing my initial values of database. Can you please help me get the desired result.
Here’s my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;
namespace AppForSis
{
public partial class AppForSis : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection myConn = new SqlConnection(
"Data Source=HOME-AA3742A183;Initial Catalog=master;"
+ "Integrated Security=True");
myConn.Open();
SqlCommand up = new SqlCommand(
@"UPDATE
[resource1]
SET
[date_entered] = @d,
[pass] = @p,
[fail] = @f,
[blocked] = @b
WHERE ([rname] = @r)
AND ([mname] = @m)",
myConn);
up.Parameters.AddWithValue("@d", date.Text);
up.Parameters.AddWithValue("@p", pass.Text);
up.Parameters.AddWithValue("@f", fail.Text);
up.Parameters.AddWithValue("@b", blocked.Text);
up.Parameters.AddWithValue("@r", resource.SelectedValue);
up.Parameters.AddWithValue("@m", module.SelectedValue);
up.ExecuteNonQuery();
myConn.Close();
GridView1.Visible = true;
}
}
}
Try to rebind the code in the Page Load but use if (!Page.IsPostBack) This means that you’ll bind the Fresh state of the DB in the first load and then rebind the changes after the update. So then, you’ll have to use GridView1.DataBind() at the end of your code in the Button_Click. Please approve this have solved your problem.