Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Collections;
using System .Collections.Generic ;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.OleDb;
using System.ComponentModel;
 
public partial class _Default : System.Web.UI.Page
{
    //class Point  { double X, Y; }
    DataTable dt = new DataTable();
   
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string connectionString = "";
        if (FileUpload1.HasFile)
        {
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
            string fileLocation = Server.MapPath("~/App_Data/" + fileName);
            FileUpload1.SaveAs(fileLocation);
            if (fileExtension == ".xls")
            {
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (fileExtension == ".xlsx")
            {
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }
            OleDbConnection con = new OleDbConnection(connectionString);
            OleDbCommand cmd = new OleDbCommand();
            ArrayList List = new ArrayList();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = con;
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
            DataTable dtExcelRecords = new DataTable();
            con.Open();
            DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
            cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
            dAdapter.SelectCommand = cmd;
            dAdapter.Fill(dtExcelRecords);
            if (Session["dtInSession"] != null)
            {
                dt = (DataTable)Session["dtInSession"];
            }
            for (int i = 0; i < dtExcelRecords.Rows.Count; i++)
            {
                int [] X;
                int [] Y;
                //if you want to get the string
                DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
                int[] X = Convert.ToInt32(thisRow["X"]);
              int[]   Y = Convert .ToInt32(thisRow["Y"]);
                //double Length = Convert.ToDouble(Y);
              //double X1 = Convert.ToDouble(X);
              //double Y1 = Convert.ToDouble(Y); 
 
                DataRow dr = dt.NewRow();
                
            }
 //polygonArea();
        }
    }
    //calculation
    private Double polygonArea(int[] X, int[] Y)
    {
        Double area = 0.0;
        int j = X.Length - 1;
        for (int i = 0; i < X.Length; i++)
        {
            area = area + (X[j] + X[i] * (Y[j] - Y[i]));
            j = i;
        }
        area = area / 2;
        if (area < 0)
            area = area * -1;
        return area;
    }
 
}
Posted
Updated 3-Oct-13 20:09pm
v2
Comments
Thanks7872 4-Oct-13 2:10am    
Don't post same code over and over again. You are advised so many times to learn some basics and then come back with concrete question.You are posting same code with different questions.

C#
int [] X;
int [] Y;
//if you want to get the string
DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
int[] X = Convert.ToInt32(thisRow["X"]);
int[] Y = Convert .ToInt32(thisRow["Y"]);

Hi, you have initialize int [] X and int [] Y twice which results to an error:
Quote:
A local variable named 'X' is already defined in this scope

Delete this:
C#
int [] X;
int [] Y;


Hope it helps! :)
 
Share this answer
 
v2
Comments
[no name] 4-Oct-13 3:10am    
Cannot implicitly convert type 'int' to 'int[]'
am tried above code and again getting this am changes so many times but no progress am tried so many ways
berrymaria 4-Oct-13 3:23am    
So far as I read your code, the reason why you got the error "A local variable named 'X' is already defined in this scope" is because you initialize int []X twice. so with int[]Y
[no name] 4-Oct-13 3:55am    
give some examples how to declare array from excel file value i know what'z the problem going on in calculation part i was declare (x,y) values as Arrays but in reading Excell values one by one that is the mistake
C#
int [] X;
int [] Y;
//if you want to get the string
DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
int[] X = Convert.ToInt32(thisRow["X"]);
int[]   Y = Convert .ToInt32(thisRow["Y"]);
//double Length = Convert.ToDouble(Y);
//double X1 = Convert.ToDouble(X);
//double Y1 = Convert.ToDouble(Y); 

DataRow dr = dt.NewRow();

Firstly you are declaring the arrays X and Y twice, which is illegal; declare them at the point you will use them. Secondly you are trying to initialize an array with a single element value, which is again illegal. And thirdly, the last line declaring the variable dr serves no purpose whatsoever.

Your code should be something like:
C#
//if you want to get the string
DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
int X = Convert.ToInt32(thisRow["X"]);
int Y = Convert.ToInt32(thisRow["Y"]);

However, there is still no provision for the situation when the returned value is not a valid integer, nor are these values of any use since they are local to the for loop and will go out of scope and be lost as soon as the loop ends. I would suggest going back to your reference manuals and reading up on scope and arrays at the minimum.
 
Share this answer
 
C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
    string connectionString = "";
    if (FileUpload1.HasFile)
    {
        string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
        string fileLocation = Server.MapPath("~/App_Data/" + fileName);
        FileUpload1.SaveAs(fileLocation);
        if (fileExtension == ".xls")
        {
            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        }
        else if (fileExtension == ".xlsx")
        {
            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        }
        OleDbConnection con = new OleDbConnection(connectionString);
        OleDbCommand cmd = new OleDbCommand();
        ArrayList List = new ArrayList();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;
        OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
        DataTable dtExcelRecords = new DataTable();
        con.Open();
        DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
        cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
        dAdapter.SelectCommand = cmd;
        dAdapter.Fill(dtExcelRecords);
        if (Session["dtInSession"] != null)
        {
            dt = (DataTable)Session["dtInSession"];
        }
        for (int i = 0; i < dtExcelRecords.Rows.Count; i++)
        {
           
            //if you want to get the string
          DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
          int  X = Convert.ToInt32  (thisRow["X"]);
          int  Y = Convert.ToInt32 (thisRow["Y"]);
         
        }
//polygonArea();
    }
}
//calculation
private Double polygonArea(int X, int Y)
{
    Double area = 0.0;
    int j = X.Length - 1;
    for (int i = 0; i < X.Length; i++)
    {
        area = area + (X[j] + X[i] * (Y[j] - Y[i]));
        j = i;
    }
    area = area / 2;
    if (area < 0)
        area = area * -1;
    return area;
}

}

Errors ://
'int' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)
 
Share this answer
 
v2
Comments
This is not an answer. Please delete this and if you want to update your question, then click on "Improve question" link.

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