I m using SqlDataAdapter to fetch data from database and display the data in textboxes. After that i want to edit the data in the textboxes itself and save back the changed data to the same database. For that i m using SqlCommand. But its taking the already fetched data only, and updating it to the database, hence showing no change to the data.
Below are some sample code:
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd;
SqlDataAdapter sda;
int counterid;
protected void Page_Load(object sender, EventArgs e)
{
conn.Open();
counterid = Convert.ToInt32(Session["counter"].ToString()); //taking counterid from session which was made in previous page.
sda = new SqlDataAdapter("select * from Counters where CounterID=@counterid", conn);
sda.SelectCommand.Parameters.Add("@counterid", SqlDbType.Int).Value = counterid;
DataTable dt = new DataTable();
sda.Fill(dt);
lblCounterId.Text = dt.Rows[0]["CounterID"].ToString();
txtCounterName.Text = dt.Rows[0]["CounterName"].ToString();
conn.Close();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
conn.Open();
cmd = new SqlCommand("update Counters set CounterName=@countername where CounterID=@counterid", conn);
cmd.Parameters.Add("@counterid", SqlDbType.Int).Value = counterid;
cmd.Parameters.Add("@countername", SqlDbType.VarChar).Value = txtCounterName.Text;
cmd.ExecuteNonQuery();
conn.Close();
Response.Redirect("ManageCounterSearch.aspx");
Session["counter"] = null;
counterid = 0;
}
Loading
Suthish NairPosted Jun 23, 2011, 10:48 AM
KajinPosted Jun 23, 2011, 6:13 AM
Suthish NairPosted Jun 23, 2011, 2:01 AM
Pradeep ChandrakerPosted Jun 22, 2011, 10:28 PM
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
conn.Open();
counterid = Convert.ToInt32(Session["counter"].ToString()); //taking counterid from session which was made in previous page.
sda = new SqlDataAdapter("select * from Counters where CounterID=@counterid", conn);
sda.SelectCommand.Parameters.Add("@counterid", SqlDbType.Int).Value = counterid;
DataTable dt = new DataTable();
sda.Fill(dt);
lblCounterId.Text = dt.Rows[0]["CounterID"].ToString();
txtCounterName.Text = dt.Rows[0]["CounterName"].ToString();
conn.Close();
}
}
KajinPosted Jun 22, 2011, 9:22 PM
KajinPosted Jun 22, 2011, 9:20 PM
Pradeep ChandrakerPosted Jun 22, 2011, 2:35 PM
Suthish NairPosted Jun 22, 2011, 2:19 PM
Pradeep ChandrakerPosted Jun 22, 2011, 2:14 PM
You need to just check what is the value you are passing when doing update. Put break point on "btnUpdate_Click" and check "txtCounterName.Text" value.
Jaganathan BantheswaranPosted Jun 22, 2011, 12:29 PM
Try like this,
string updateSql = "UPDATE Employees " + "SET LastName = @LastName " + "WHERE FirstName = @FirstName";
SqlCommand UpdateCmd = new SqlCommand(updateSql, thisConnection);
UpdateCmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10, "FirstName");
UpdateCmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 20, "LastName");
UpdateCmd.Parameters["@FirstName"].Value = "Wade";
UpdateCmd.Parameters["@LastName"].Value = "Harvey";
UpdateCmd.ExecuteNonQuery();