Friday 29 July 2011

Broken Function: There are some problems with the code below. Identify at least 4 issues.

private void GetCustomers()
{

System.Text.StringBuilder sBuilder = new System.Text.StringBuilder("");
System.Data.SqlClient.SqlDataReader oDR;
String sOutput;
System.Data.SqlClient.SqlConnection oConn;

try
{
System.Data.SqlClient.SqlCommand oCmd = new System.Data.SqlClient.SqlCommand("usp_GetCustomerNames",oConn);
                   
oCmd.CommandType = CommandType.StoredProcedure;
oDR = oCmd.ExecuteReader(CommandBehavior.CloseConnection);

while (oDR.Read)
{
sBuilder.Append(oDR("ForeName") & ";");
}
               
}
catch (Exception ex)
{

}

oDR.Close();

return sOutput;
}




ANS:


1) System.Data.SqlClient.SqlConnection oConn;
The connection object declared but then is not instantiated with a valid connection string,
for eample oConn.ConnectionString = “Data Source=(local);Database=AdventureWorks;Integrated Security=SSPI”


2)System.Text.StringBuilder("");
StringBuilder constructor arguments cannot be empty string

3) return sOutput;
No valid string is assigned to the sOutput variable returned by the GetCustomers() function.

4)
catch (Exception ex)
{

}
There is no code written to deal with the exception in the try/catch exception handler.
for example, to display a message to the user, or cancel the exception in order to allow processing to continue uninterruppted.

No comments:

Post a Comment