C# .NET - Verify a value using a textbox in the table

Asked By Daniel on 12-Feb-12 10:47 PM
Hi!I want my textbox to check weather the value exists or not into a table and verifier also if field 'etat'  is not null:
I am using the following code to perform that but when enter a value which exist and the field 'etat' is not null, it goes to the else condition and return 'TransactionID does not exists'
:
protected void TextBoxTransactionID_TextChanged(object sender, EventArgs e)
        {
            if (TextBoxTransactionID.Text == "SELECT [idtrans] FROM [TransactionM] WHERE idtrans IS NOT NULL AND etat IS NOT NULL")
            {
                
                GridView1.Visible = true;
                LabelMessage.Text = "<font color='green'>TransactionID exists!</font>";
            }
            else
            {
                LabelMessage.Text = "<font color='red'>TransactionID does not exists!</font>";
            }
        }

can someone help fixing this!
Web Star replied to Daniel on 12-Feb-12 11:10 PM
Yes, you are writing wrong if condition even you put anything in textbox that will go else part of if every time because
in this line you are putting sql script directly it didn't evaluate value until unless you are fetch the Transaction id from database
by complete ADO.net code
in your condition it is comparing you entered id with sql script which will return always false so it goes in else part
if (TextBoxTransactionID.Text == "SELECT [idtrans] FROM [TransactionM] WHERE idtrans IS NOT NULL AND etat IS NOT NULL") 


Amit Choudhary replied to Daniel on 12-Feb-12 11:14 PM

The query you have written will bring some rows where both values(idtrans ,etat ) are not null.
Since the event you are using will be fired every time you are typing in text box , if any value is not equal to typed string in text box (checking of equal condition is also case sensitive) , it means it will go to else condition. .
you can debug the code and verify it.

Daniel replied to Web Star on 12-Feb-12 11:20 PM
Please help how can I resolve that?
Chintan Vaghela replied to Daniel on 13-Feb-12 12:25 AM

Hello,

 

Try your code as following way

SqlCommand cmd = new SqlCommand("SELECT [idtrans] FROM [TransactionM] WHERE idtrans =" + TextBoxTransactionID.Text + " AND etat IS NOT NULL", conn);

 

      conn.Open();

      if (cmd.ExecuteScalar() == System.DBNull.Value)

      {

        LabelMessage.Text = "<font color='red'>TransactionID does not exists!</font>";

     

      }

      else

      {

        GridView1.Visible = true;

        LabelMessage.Text = "<font color='green'>TransactionID exists!</font>";

      }

 

 

Hope this is helpful !

Thanks

 

 

 

 

 

Somesh Yadav replied to Daniel on 13-Feb-12 01:56 AM
Hi try like the below one.

SqlCommand cmd = new SqlCommand("SELECT [idtrans] FROM [TransactionM] WHERE idtrans =" + TextBoxTransactionID.Text + " AND etat IS NOT NULL", conn);

 

    conn.Open();

    if (cmd.ExecuteScalar() == System.DBNull.Value)

    {

      LabelMessage.Text = "<font color='red'>TransactionID does not exists!</font>";

     

    }

    else

    {

      GridView1.Visible = true;

      LabelMessage.Text = "<font color='green'>TransactionID exists!</font>";

    }

Hope it helps you.
dipa ahuja replied to Daniel on 13-Feb-12 02:47 AM
Markup
 
<asp:UpdatePanel runat="server" ID="up1">
  <ContentTemplate>
    Username:
    <asp:TextBox runat="server" ID="Username" AutoPostBack="true" OnTextChanged="Username_Changed" />
    <div runat="server" id="UserAvailability">
    </div>
    <br />
  </ContentTemplate>
</asp:UpdatePanel>
 
Code Behind:
 
protected void Username_Changed(object sender, EventArgs e)
{
  if (!checkExist(Username.Text))
  {
    UserAvailability.InnerText = "exist";     
  }
  else
  {
    UserAvailability.InnerText = "not exist";
  }
}
 
public bool checkExist(string s)
{
  string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
   
  SqlDataAdapter da = new SqlDataAdapter("Select name from people where name='"
    + s + "'", conn);
 
  DataTable dt = new DataTable();
  da.Fill(dt);
 
  if (dt.Rows.Count > 0)
  {
    return false;
  }
  else
  {
    return true;
  }
}
 
Web Star replied to Daniel on 13-Feb-12 04:11 AM
simply fetch the id from database using ado.net and than put your condition to set visibility of control

SqlCommand cmd = new SqlCommand("SELECT [idtrans] FROM [TransactionM] WHERE idtrans =" + TextBoxTransactionID.Text + " AND etat IS NOT NULL", conn);
conn.Open();
int i = cmd.ExecuteScalar();


            if (i>0)
            {
                
                GridView1.Visible = true;
                LabelMessage.Text = "<font color='green'>TransactionID exists!</font>";
            }
            else
            {
                LabelMessage.Text = "<font color='red'>TransactionID does not exists!</font>";
            }