I have two buttons (btn_Out and btn_In). I want to display btn_Out if the value of field Active in an SQL table called Machines is True (1) and display btn_In if the value of Active is set to False (0).
Every row in the data in the gridview may have a different Active flag and so the button needs to reflect this. The button will change the active flag from 0 to 1 and vice versa (i have this working!).
I am using a gridview and my code is as follows:
ASP.NET
<ItemTemplate>
<asp:Button ID="btn_In" runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
</ItemTemplate>
<ItemTemplate>
<asp:Button ID="btn_Out" runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
</ItemTemplate>
C#:
DataTable dt = new DataTable();
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Open();
con = new SqlConnection(cs);
// SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select [active] from [ALLMACHINES].[dbo].[Machines] where [serial_number] = @serialNumber";
cmd.Parameters.AddWithValue("@Serial_Number", serialNumber);
int Active = Convert.ToInt32(cmd.ExecuteScalar());
if (Active == 1)
{
btn_In.Visible = false;
btn_Out.Visible = true;
}
else if (Active == 0)
{
btn_Out.Visible = false;
btn_In.Visible = true;
}
}
My aspx.net page doesn't like that I've used the buttons in the if and else if statement and won't compile! Any tips would be much appreciated :)