?
Solved

if else statement

Posted on 2006-07-07
8
Medium Priority
?
287 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 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