what is unreachable code detected ?

Posted by Tejamanimala under C# on 10/16/2013 | Points: 10 | Views : 7229 | Status : [Member] | Replies : 2
hi am uploading excel sheet into database,at the same time i want to insert the data from excel to database table,,,,,here is the code....but am getting the error as unreachable code detected....i am not getting this error...please help me.....in the following code in the place of if am getting this error,i make it to bold please find it...
private void PopulateDatabaseTables()
{
string tableName = string.Empty;
string sql = "SELECT *, name AS table_name " +
" FROM sys.tables WHERE Type = 'U' ORDER BY table_name";
using (SqlConnection conn = new SqlConnection(connStr))
{
using (DataTable table = new DataTable())
{
conn.Open();
using (SqlDataAdapter dAd = new SqlDataAdapter(sql, conn))
{
dAd.Fill(table);
}
ListBox1.DataSource = table;
ListBox1.DataBind();
}
}
}

protected void ImportNow_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedValue == "")
{
lblMessage.ForeColor = Color.Red;
lblMessage.Text = "Please select table in which you want to import data from excel sheet";
}
else if ((fileuploadExcel.FileName != ""))
{
string extension = Path.GetExtension(fileuploadExcel.PostedFile.FileName);
string excelConnectionString;
SqlConnection conn = new SqlConnection(connStr);
string tableName = ListBox1.SelectedValue;
string path = Server.MapPath("~/fileuploadExcel/" + fileuploadExcel.FileName);
fileuploadExcel.SaveAs(path);
Response.Write("path=" + path);
return;
if (extension == ".xlsx")
{
excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=+ path +
;Extended Properties=Excel 8.0;Persist Security Info=False";
}
else
{
excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;sData Source= + path +
;Extended Properties=Excel 12.0;Persist Security Info=False";
}

SqlConnection excelConnection = new SqlConnection(excelConnectionString);
conn.Open();
SqlCommand comm = new SqlCommand("truncate table " + tableName, conn);
SqlCommand identityChange = conn.CreateCommand();
identityChange.CommandText = "SET IDENTITY_INSERT " + tableName + " ON";
SqlCommand cmd = new SqlCommand("Select * from [Sheet1$]", excelConnection);
excelConnection.Open();
SqlDataReader dReader;
dReader = cmd.ExecuteReader();
identityChange.ExecuteNonQuery();
SqlBulkCopy sqlBulk = new SqlBulkCopy(connStr);
sqlBulk.DestinationTableName = tableName;
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
conn.Close();
lblMessage.ForeColor = Color.Green;
lblMessage.Text = "Import into table <b>" + tableName + "</b> successful!<br />";
}
else
{
lblMessage.ForeColor = Color.Red;
lblMessage.Text = "Please first upload (Select) excel file.";
}


manimala


Responses

Posted by: Learningtorise on: 10/16/2013 [Member] Starter | Points: 25

Up
0
Down
Remove return; statement before IF.


http://hashtagakash.wordpress.com/

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 10/18/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
It means code lines are not going to be executed because of some misplaced keywords or conditions.
As its in your program you can see return statement before If condition. What here happens is, once it returns value after that there will be no scope for execution of rest part after this return statement. So these code part will be called as Unreachable code. So either you've to remove this return and put it in proper place else you can set some condition for this.

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response