Solved

if else statement

Posted on 2006-07-07
8
259 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:
ciuly 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
 
LVL 28

Expert Comment

by:ciuly
ID: 17058684
the I think I didn't understand. so I ask: which part doesn't work?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows phone Development 11 29
C# Problem Solving Execerscise 5 41
Visual Studio: built-in keystroke automation 2 23
ConsoleSql 1 18
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now