Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
This is my code:
 private void txtCountryCode_Enter(object sender, EventArgs e)
        {
            try
            {
                if (txtVehClass.Text.Trim() == string.Empty)
                
                    return;
 
                    string strQry = string.Empty;
                    strQry = "Select * from VehClass where upper(code)='" + txtVehClass.Text.Trim().ToUpper() + "' and isnull(intstatus,0)=0";
 
                    DataSet dsGrid = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["TrafficDB"], CommandType.Text, strQry);
 
                    if (dsGrid != null && dsGrid.Tables.Count > 0 && dsGrid.Tables[0].Rows.Count > 0)
                    
                        txtVehClass.Tag = dsGrid.Tables[0].Rows[0]["Intcode"].ToString();
                        txtVehClass.Text = dsGrid.Tables[0].Rows[0]["code"].ToString();
                        txtVehClassName.Text = dsGrid.Tables[0].Rows[0]["NameArb"].ToString();
                    }
                }
 
            }
    }
the error is expected catch or finally...how to solve this error
Posted 14-Jan-13 20:25pm
Edited 14-Jan-13 20:32pm
Tadit Dash83.5K
v2
Comments
Shanu2rick at 15-Jan-13 2:40am
   
Include a catch block right after try block, please.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Should be like this:
 
private void txtCountryCode_Enter(object sender, EventArgs e)
{
    try
    {
        //whatever
    }
    catch (Exception exception)
    {
        //if an error occurs with in the try block, it will handled here.
    }
}
 
I would suggest you to buy a basic programming book and read the concepts.
Read more documentation here at MSDN:
 
http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.110).aspx[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

private void txtCountryCode_Enter(object sender, EventArgs e)
        {
            try
            {
                if (txtVehClass.Text.Trim() == string.Empty)
                
                    return;
 
                    string strQry = string.Empty;
                    strQry = "Select * from VehClass where upper(code)='" + txtVehClass.Text.Trim().ToUpper() + "' and isnull(intstatus,0)=0";
 
                    DataSet dsGrid = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["TrafficDB"], CommandType.Text, strQry);
 
                    if (dsGrid != null && dsGrid.Tables.Count > 0 && dsGrid.Tables[0].Rows.Count > 0)
                    
                        txtVehClass.Tag = dsGrid.Tables[0].Rows[0]["Intcode"].ToString();
                        txtVehClass.Text = dsGrid.Tables[0].Rows[0]["code"].ToString();
                        txtVehClassName.Text = dsGrid.Tables[0].Rows[0]["NameArb"].ToString();
                    }
                }
 
            }
            catch(Exception ex)
            {// do exception handling here
            }
            finally
            {// do resource cleanup here, optional in your case
            }
    }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Add a catch or finally block:
private void txtCountryCode_Enter(object sender, EventArgs e)
    {
    try
        {
        if (txtVehClass.Text.Trim() == string.Empty)
 
            return;
 
        string strQry = string.Empty;
        strQry = "Select * from VehClass where upper(code)='" + txtVehClass.Text.Trim().ToUpper() + "' and isnull(intstatus,0)=0";
 
        //DataSet dsGrid = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["TrafficDB"], CommandType.Text, strQry);

        if (dsGrid != null && dsGrid.Tables.Count > 0 && dsGrid.Tables[0].Rows.Count > 0)
            {
            txtVehClass.Tag = dsGrid.Tables[0].Rows[0]["Intcode"].ToString();
            txtVehClass.Text = dsGrid.Tables[0].Rows[0]["code"].ToString();
            txtVehClassName.Text = dsGrid.Tables[0].Rows[0]["NameArb"].ToString();
            }
        }
    catch (Exception ex)
        {
        // Report, log or otherwise handle the exception
        Console.WriteLine(ex.Message);
        }
 
    }
Do note that you are also missing an open curly bracket after the if statement - indentation will not make it execute all three statements without it! (I added it here so you can see what I mean)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 9,225
1 OriginalGriff 5,395
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid