Click here to Skip to main content
15,611,775 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
this code is working fine but it is not checked the empty condition when user not fill item description.



private void btnSave_Click(object sender, EventArgs e)
       {
           string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
           SqlDataReader reader = null;
           SqlConnection conn = null;
           try
           {
               string query = "insert into ItemTable values('" + cmbItemType.Text +  "','" + txtItemCode.Text + "','" + txtItemDescription.Text + "','" + txtLaborCharge.Text + "')";
               conn = new SqlConnection(connstr);

               if (txtItemDescription.Text != "" & txtItemCode.Text != "")
               {
                   conn.Open();
                   //Checking User Name Exists in CustomerTable
                   string query1 = "select txtItemCode from ItemTable where txtItemCode='" + txtItemCode.Text + "'";
                   SqlCommand cmd = new SqlCommand(query1, conn);
                   reader = cmd.ExecuteReader();
                   if (reader != null && reader.HasRows)
                   {
                       //User exists in db do something
                       MessageBox.Show("Item Already Exists!!");
                   }
                   else
                   {
                       if (reader != null)
                       {
                           reader.Close(); // close the reader before making a new connection
                       }
                           SqlCommand cmd1 = new SqlCommand(query, conn);
                           cmd1.ExecuteNonQuery();
                           txtItemDescription.Clear();
                           txtLaborCharge.Clear();
                           txtItemCode.Clear();
                           MessageBox.Show("Values Save in DataBase");

                   }
                   reader.Close();
                   conn.Close();
               }
               else
               {
                   MessageBox.Show("Only Labor Charge Can be Empty");
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("At Least 0 Value In Labor Charge", ex.Message);
           }
           finally
           {
               if (reader != null)
               {
                   reader.Close();
               }
               if (conn != null)
               {
                   conn.Close();
               }
           }
       }
Posted
Comments
[no name] 4-Sep-13 10:12am    
You are missing an & in your if statement.
Manish Arya 4-Sep-13 10:21am    
thanks

1 solution

Just to get this off the unanswered list

You need to add a & to your if statement

if (txtItemDescription.Text != "" & txtItemCode.Text != "")


should be
if (txtItemDescription.Text != "" && txtItemCode.Text != "")


& by itself is the logical AND operator described here[^]

What you really want is the conditional AND described here[^]
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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