Solved

if else statement

Posted on 2006-07-07
8
267 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
  • 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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