Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
con1.Open();
SqlCommand cmd = new SqlCommand("Select first from TransactionsInfo where name=@name and tid=@tid", con1);
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@tid", txtId.Text);
SqlDataReader rdr = cmd.ExecuteReader();

if (!DBNull.Value.Equals(rdr["first"]))
{
    MessageBox.Show("First payment already exists in database .", "Alert !", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
//save
}



I want to validate if my field "first" in TransactionsInfo table has value or null
ty in advance :)
Posted
Updated 29-May-14 17:02pm
v2
Comments
syed shanu 29-May-14 23:04pm    
so what is the problem yiou can check null value in sql query itself
select ISNULL(first ,'') from TransactionsInfo where name=@name and tid=@tid
Member 10717094 29-May-14 23:07pm    
do i have to use sqldatareader and what condition to use?
thanks for ur reply

1 solution

C#
//check for rows 
if (rdr.HasRows)
{            
    // for reading one row
    if (rdr.Read())
    {
        if(dr1["first"] != DBNull.Value)
       {
            MessageBox.Show("First payment already exists in database .", "Alert !", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       }else
       {
          // dr1["first"] has DBNull
       }
    }
}else
{
   // no rows returned by the reader, show message....
}
 
Share this answer
 
v4
Comments
Member 10717094 29-May-14 23:17pm    
thanks Damith it works fine
DamithSL 29-May-14 23:19pm    
You are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900