Hi NerakSeven,
Check this example. Now please take its reference and correct your code.
SQL
CREATE TABLE Movies(ID INT IDENTITY PRIMARY KEY,Name VARCHAR(50),Flag CHAR(5),Code INT)
INSERT INTO Movies VALUES('Machuca','true',11)
INSERT INTO Movies VALUES('Gloria','true',7)
INSERT INTO Movies VALUES('The Life Of Fish','true',7)
INSERT INTO Movies VALUES('My Best Enemy','true',11)
HTML
<asp:Button Text="Update" runat="server" OnClick="Save" />
Namespaces
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
Code
private void Update(string C_E, int id)
{
string query = "UPDATE Movies SET Flag = @code WHERE ID = @Id";
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand command = new SqlCommand(query, con);
command.Parameters.Add("@code", SqlDbType.NChar).Value = C_E;
command.Parameters.Add("@Id", SqlDbType.Int).Value = id;
con.Open();
command.ExecuteNonQuery();
con.Close();
}
}
protected void Save(object sender, EventArgs e)
{
string query = "SELECT * FROM Movies";
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
if (sdr["Code"].ToString() == "11")
{
this.Update("true", Convert.ToInt32(sdr["ID"]));
}
else if (sdr["Code"].ToString() == "7")
{
this.Update("false", Convert.ToInt32(sdr["ID"]));
}
}
}
con.Close();
}
}
Output
Before Update
ID |
Name |
Flag |
Code |
1 |
Machuca |
true |
11 |
2 |
Gloria |
true |
7 |
3 |
The Life Of Fish |
true |
7 |
4 |
My Best Enemy |
true |
11 |
After Update
ID |
Name |
Flag |
Code |
1 |
Machuca |
true |
11 |
2 |
Gloria |
false |
7 |
3 |
The Life Of Fish |
false |
7 |
4 |
My Best Enemy |
true |
11 |