Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code:
C#
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
v2
Comments
Shanu2rick 15-Jan-13 2:40am    
Include a catch block right after try block, please.

Should be like this:

C#
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[^]
 
Share this answer
 
C#
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
            }
    }
 
Share this answer
 
Add a catch or finally block:
C#
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)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900