Hi, on my asp.net page I have a search box, a label and a database connection.
I am trying to show or hide the label depending on the data inputted into the search box.
For example if a user enters ID number 3452 into the search box and it is not in the database the label should be visible, however if it is a valid ID within the database the label should be hidden.
I have the following code in my system however the label is showing even if the ID entered is in the database is valid.
if (TextBox50.Text != "CCARNo")
{
Label5.Visible = true;
}
else
{
Label5.Visible = false;
}
Does anyone know what I must do to get the code working as expected?
Thank you for your help.
Loading
Satyapriya NayakPosted Apr 25, 2013, 10:40 AM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Show_label_based_on_seach
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select count(*)from test where id='" + txtId.Text + "'";
com = new SqlCommand(str, con);
int count = Convert.ToInt32(com.ExecuteScalar());
if (count > 0)
{
Label1.Visible = false;
}
else
{
Label1.Visible = true;
}
}
protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible = false;
}
}
}