An unreachable statement error - unreachable-statement

I don't know from where the error is coming "unreachable statemententer image description here"

You have a return statement before if statement. If you return, how will your if statement be executed. Hence if become unreachable.
Remove this line:
return new String[0];

Related

ConditionalCheckFailed in DynamoDb

While inserting the entry into the dynamodb table, I am getting the follwing error:
com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException:
The conditional request failed (Service: AmazonDynamoDBv2; Status
Code: 400; Error Code: ConditionalCheckFailedException;
This is my Java code for inserting the element:
DynamoDBSaveExpression expression = (new DynamoDBSaveExpression()).withExpectedEntry("itemKey", new ExpectedAttributeValue(false));
try {
this.mapper.save(itemKeyMapping, expression);
} catch (Exception var3) {
this.logger.error("", var3);
throw var3;
}
itemKeyMapping is an object which has a member variable as itemKey.
So, it should work. The condition is just saying that itemKey should exist, right?
Please let me know if I am doing some mistake.
My understanding is here was wrong. Actually, condition above means that insert only when itemKey attribute value in the database does not exist.

Why does try catch not catch Sql syntax error

I'm adding an inputbox to my site where I as the admin can enter sql select statements, I put a try catch around the code that executes the select to try and catch syntax errors but even with this my site goes to the "error in application / " page when there is a syntax error.
I don't understand what I am doing wrong. I am developing on Asp.net v4.
The code that executes the custom SQL command is as follows;
try
{
//edtSQL.Text = "WHRE Field='Value'"
//The Resulting SQL Command will be incorrect because of incorrect syntax
SqlDataSource1.SelectCommand = "SELECT * FROM DataTable " + edtSQL.Text;
SqlDataSource1.Select(new DataSourceSelectArguments());
}
catch(Exception ex)
{
//Bad Syntax should be caught here, but it is not. This never get called
// even when there is a syntax error.
lblQueryStatus.Text = "Error, can't execute SQL statment";
}
Instead if the label showing the error message, the site gives an error and goes to the default site error page.
The catch in the above code does actually catch the exception. On the exception set the Select Command to something default that is sure to work ie.
SELECT * FROM Database
And Execute the select. The reason for this is when the page does a post back it tries to execute the select statement that has been set for it and there is not try catch around this(because its in another context) and this is why the server then defaults to the "error in application / " page.
The code should look like this
try
{
//edtSQL.Text = "WHRE Field='Value'"
//The Resulting SQL Command will be incorrect because of incorrect syntax
SqlDataSource1.SelectCommand = "SELECT * FROM DataTable " + edtSQL.Text;
SqlDataSource1.Select(new DataSourceSelectArguments());
}
catch(Exception ex)
{
SqlDataSource1.SelectCommand = "SELECT * FROM DataTable"; //This will work for sure
SqlDataSource1.Select(new DataSourceSelectArguments());
lblQueryStatus.Text = "Error, can't execute SQL statment";
}

runtime error in running asp.net application

Am running asp.net application with access database using gridview application..while running i got the run time error as
Object reference not set to an instance of an object.
Line 41: RadioButtonList rblGender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("rbGenderEdit");
Line 42:DropDownList ddlStatus = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatusEdit");
Line 43:SqlDataSource1.UpdateParameters["Sex"].DefaultValue = rblGender.SelectedValue;
Line 44:SqlDataSource1.UpdateParameters["MaritalStauts"].DefaultValue = ddlStatus.SelectedValue;
Line 45: }
I got this error specially in line 43..
So rblGender.SelectedValue or rblGender is null...
Perhaps the problen is with rblGender
Make assignment as follows:
RadioButtonList rblGender = GridView1.Rows[e.RowIndex].FindControl("rbGenderEdit") as RadioButtonList;
And then check for nullability:
if (rblGender == null)
{
//show error
}
RadioButtonList rblGender = (RadioButtonList)GridView1.Rows[e.RowIndex].TemplateControl.FindControl("rbGenderEdit");
if it is in template field.
it can't find rbGenderEdit.
When you have a runtime error like this one, you should use your debugger to actually see what is going on behind the scene. Just put your breakpoint at the line 43 for instance, run your program in debug mode and start investigate to see what object has a null reference and try to fix it.
For instance take a look at the line 41, rblGender might be null...
EDIT
You must check if the objects that you are manipulating are null, this is part of the defensive programming techniques.
In your example, as others have said you can do it this way:
if(rblGender == null) {
// If you are running your program with a console
// Otherwise you should display this anywhere you can or in a log file.
Console.WriteLine("rblGender is null");
}
else if(rblGender.SelectedValue == null) {
Console.WriteLine("rblGender.SelectedValue is null");
}
Run your program and check what is being written! This is not going to solve your problem, but it is just going to tell you where your null reference is which will help you figure out what should be fixed!
But as I said earlier you can also debug your program properly by putting a breakpoint (you know the red ball when you click on the side of the window) at the line 43, and then run your program in debug mode! When the runtime error will fireup you will be able to check whether rblGender or rblGender.SelectedValue is null.
Also, from a more general point of view, checking your object against null references will prevent your application from crashing suddenly by managing the case where an objet might have a null reference at any given time. For instance you can say:
if(my_object is null)
{
myValue = "default";
}
else
{
myValue = my_objet.getValue();
}
This is just an example it could be done in a better way, using exceptions (try/catch/finally) for instance but the general idea is: check against null references!

Debug Mode: See what is in SqlCommand

I'd like to see what is about to be sent to the SQL Server from my SqlCommand before SQLCmd.ExecuteNonQuery() runs.
I'm trying to debug because I am receiving the following error: System.FormatException: Failed to convert parameter value from a String to a Int32.
Normally, I would use SQL Server Profiler to view what is being sent to SQL Server, but my statement is not making it that far.
Is there a way to determine what it is trying to convert? I am having problems determining which parameter is causing the error.
There's nothing that can quickly visualize it for you, but you can browse through the object and get at the internal list of parameters and view their names and values individually.
It will likely be faster just to write something that you can pass the command into and it will print out the name, DbType, Value.ToString() and Value.GetType().Name for each parameter.
void PrintCommand(DbCommand command)
{
Console.WriteLine("CommandType: {0}", command.CommandType);
Console.WriteLine("CommandText: {0}", command.CommandText);
foreach(var parameter in command.Parameters)
{
Console.WriteLine(" Parameter {0}, {1}: \"{2}\" ({3})",
parameter.ParameterName,
parameter.DbType,
parameter.Value,
parameter.Value.GetType().Name);
}
}
Try the immediate window:
? SQLCmd.Parameters["#MyParam"].Value

Value passed with request.setAttribute() is not available by request.getParameter()

I give a string variable a value in the normal execution of the code ,but if an exception happen I will give it another value , the problem is that in catch block the value is still the same as i assign first .
Here is my code ,first I assign page value "addUser" inside try block and in catch I give it "ErrorPage" value , I send the value of page within http request to forword method and inside it i print the value of page.
I cause an error in the excution of the code an i want it to go through catch block , and it does , but when it send the page value to the forword function the value of page is "addUser" not "ErrorPage" although i assign it to "ErrorPage" !!
String page = "addUser";
try {
// ...
request.setAttribute("page", page);
forward(request, response);
} catch (SQLException e) {
page = "ErrorPage";
request.setAttribute("page", page);
forward(request, response);
}
and here is the forword function
String page = request.getParameter("page");
System.out.println("page is " + page); // each time it prints addUSer
Can someone help? and thanx in advance.
You're calling request.getParameter() instead of request.getAttribute() to obtain the value. Since you've set it as request attribute, you should also get it as request attribute.
So:
request.setAttribute("foo", foo);
is only available by
Object foo = request.getAttribute("foo"); // NOT getParameter().
The getParameter() is only for HTTP request parameters as you can specify in request URL or in input fields of HTML forms.
In addition to BalusC's point"
In your code you have declared two "String page" variable. This will not compile. I think the parameter that you are passing to the request in the catch must be the "other" page variable. But it is hard to tell since this is not a true example.

Resources