Solved

if else statement

Posted on 2006-07-07
8
275 Views
Last Modified: 2010-04-16
Hi experts,

this piece of code checks to see if there is a
duplicate data in the database

=========================
public bool checkTextBox(string str)
            {
            con.Close();
            SqlParameter[] paramsArray = new SqlParameter[1];
            con.Open();
                  SqlCommand sCommand = new SqlCommand("checkDuplicateEmpData",con);
                  sCommand.CommandType = CommandType.StoredProcedure;
                  sCommand.Parameters.Add("@employerId",str);
                  SqlDataReader rdr = sCommand.ExecuteReader();
                  if(rdr.Read())
                  {
                        return true;
                  }
                  else
                        return false;
                  con.Close();
            }



I have called the above piece of code, but not quite!!

How do I say that if the value of the above code is true then do something
else
if
then
hide the button
and display error

here is what I HAVE SO FAR but it works regardless of the bool checkTextBox
being true or false.

=====
try
                  {
      //if (postedFile != null && postedFile.FileName.Length > 0 && this.ddEmployers.SelectedValue.Length > 0 && this.ddEmployers.SelectedValue != "Click to select a client from the list")
                  
      if(checkTextBox(txtAtlasID.Text.ToString()))
                        
      Button1.Visible=false;
      lblErrorReport.Text = "Employer ID already exists in the Database.";
      
                              
      {
======

full code is
============================


=============================

thanks guys. alway grateful for your help.


private void getSomething()
            {
                  
                  HttpPostedFile postedFile = null;                                                      
                  conn = new OleDbConnection();
                  
                  bool importSucceeded = false;
                  
                  DataTable dt;
                  string[] wrkshtNames;

                  int i = 0;
                  
                  this.divGrid.Style.Add("display","none");
                  postedFile = this.inpAttach.PostedFile;
                  try
                  {
                  
                        //if (postedFile != null && postedFile.FileName.Length > 0 && this.ddEmployers.SelectedValue.Length > 0 && this.ddEmployers.SelectedValue != "Click to select a client from the list")
                  
                        if(checkTextBox(txtAtlasID.Text.ToString()))
                        
                              //Button1.Visible=false;
                              //lblErrorReport.Text = "Employer ID already exists in the Database.";
                              
                        //  Response.Write("lblErrorReport");
                              
                        {
                              //this.lblErrorReport.Text = "";
                              this.lblfilename.Text = Path.GetFileName(postedFile.FileName);
                              //                              postedFile.SaveAs("C:\\code\\" + Path.GetFileName(postedFile.FileName));

                              postedFile.SaveAs(HttpContext.Current.Server.MapPath("/AutoDataCapture")+ "\\Files\\" + Path.GetFileName(postedFile.FileName));
                              
                              if (postedFile.ContentType == "application/vnd.ms-excel")
                              {
                                    conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("/AutoDataCapture") + "\\Files\\" + Path.GetFileName(postedFile.FileName) +";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");                              
                                    //                                    conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\code\\" + lblfilename.Text +";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");
                                    conn.Open();
                                    dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
                                    if (dt != null)
                                    {
                                          wrkshtNames = new string[dt.Rows.Count];
                                          foreach(DataRow row in dt.Rows)
                                          {                                                                                                                                                
                                                wrkshtNames[i] = row["TABLE_NAME"].ToString().TrimEnd('\'').TrimStart('\'');
                                                i++;                                                                                                                        
                                          }                                                                                                                                                                                                                                                                        
                                          conn.Close();
                                          importSucceeded =  this.importSheetData(wrkshtNames);
                                          
                                    }                                                                                                            
                                                            
                              
                              }
                              else this.lblErrorReport.Text = "The input file must be a valid MS Excel spreadsheet and ensure that the file is not in use by you or another user on the network.";
                        }
                  //      else this.lblErrorReport.Text = "Please select an MS Excel input file and select an employer from the dropdown list";
                        
                  }
                  catch(Exception ex)
                  {
                        Response.Write(ex.Message);
                  }

                  finally
                  {
                        conn = null;
                  }
                  
            }
0
Comment
Question by:SirReadAlot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 500 total points
ID: 17058634
first, before con.Close(); do rdr.Close();

are you sure that you call checkTextBox correctly? because if it return always false, then of course your code does not work.

so you will have to check in your excel file and make sure that the employerid you pass as argument does exists and that the stored procedure does the correct thing (maybe there is an error in there which makes it return an empty dataset.
0
 

Author Comment

by:SirReadAlot
ID: 17058652
will check
0
 

Author Comment

by:SirReadAlot
ID: 17058667
this bit works

if(checkTextBox(txtAtlasID.Text.ToString()))
                        {
                              Button1.Enabled=false;
                              lblErrorReport.Text = "Employer ID already exists in the Database.";
                              
                              //Response.Write("lblErrorReport");
                        }
                        else

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:2266180
ID: 17058684
the I think I didn't understand. so I ask: which part doesn't work?
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17058700
Hi SirReadAlot,

Not quite sure fo your intended order of if/else blocks.
I'll just give a generic example based on your code.

Note: There is not "Then" in C only if, else
=====
try          
{
     //if (postedFile != null && postedFile. // TRUNCATED
               
    if(checkTextBox(txtAtlasID.Text.ToString<wbr/>()))
    {
        Button1.Visible=false;
        lblErrorReport.Text = "Employer ID already exists in the Database.";
    }
    else
    {
        if (/*some condition*/)
        {
              MsgBox("Doing something else")
        }
        else
        {
             MsgBox("NOT Doing something else")
        }
    }                      
}
 catch (...)
{
   blErrorReport.Text = "Some serious error occured"
======

Cheers!
Sebastian
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17058710
}

// missing closing '}' for catch statement
0
 

Author Comment

by:SirReadAlot
ID: 17058730
the I think I didn't understand. so I ask: which part doesn't work?


the part that is suppose to import the files
0
 
LVL 11

Expert Comment

by:vo1d
ID: 17058863
change your function to this:

public bool checkTextBox(string str)
{
  SqlCommand sCommand = null;
  SqlDataReader rdr = null;
  bool result = false;

  try
  {
    if(con.State != ConnectionState.Closed)
    {
      con.Close();
    }
 
    SqlParameter[] paramsArray = new SqlParameter[1];
    con.Open();
    sCommand = new SqlCommand("checkDuplicateEmpData",con);
    sCommand.CommandType = CommandType.StoredProcedure;
    sCommand.Parameters.Add("@employerId",str);
    rdr = sCommand.ExecuteReader();  
    result = rdr.HasRows;
  }
  catch(Exception ex)
  {
    //evaluate the exception if you want  
    result = false;
  }
  finally
  {
     if(!rdr.IsClosed)
     {
       rdr.Close();
     }
     con.Close();
     sCommand.Dispose();
  }
  return (result);
}


now you can do this in your mainprog:

if((Button1.Visible = checkTextBox(txtAtlasID.Text.ToString())))
{
   //place your code here for a visible checkbox  -> checkTextBox returned true;
}
else
{
  //place your code here for a hidden checkbox  -> checkTextBox returned false;
}

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Set custom font WPF 2 25
c#, case, if 4 37
Windows Universal App 22 58
msiexec won't run 4 24
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question