Solved

if else statement

Posted on 2006-07-07
8
262 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

776 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