0

I am Importing Data in Student Record from excel sheet with .xls as it's file extension when i am calling the function to upload Excel that is

  public ActionResult Importexcel()
        {


            if (Request.Files["FileUpload1"].ContentLength > 0)
            {
                string extension = System.IO.Path.GetExtension(Request.Files["FileUpload1"].FileName);
                string path1 = string.Format("{0}/{1}", Server.MapPath("~/Content/UploadedFolder"), Request.Files["FileUpload1"].FileName);
                if (System.IO.File.Exists(path1))
                    System.IO.File.Delete(path1);

                Request.Files["FileUpload1"].SaveAs(path1);
                string sqlConnectionString = @"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-UserInfo-20140318063343.mdf;Initial Catalog=aspnet-UserInfo-20140318063343;Integrated Security=True";


                //Create connection string to Excel work book
                string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path1 + ";Extended Properties=Excel 12.0;Persist Security Info=False";
                //Create Connection to Excel work book
                OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);

                //Create OleDbCommand to fetch data from Excel
                OleDbCommand cmd = new OleDbCommand("Select [Id],[Name],[StudentId] from [Sheet1$]", excelConnection);

                excelConnection.Open();
                OleDbDataReader dReader;
                dReader = cmd.ExecuteReader();

                SqlBulkCopy sqlBulk = new SqlBulkCopy(sqlConnectionString);
                //Give your Destination table name
                sqlBulk.DestinationTableName = "StudentRecords";
                sqlBulk.WriteToServer(dReader);
                excelConnection.Close();

                // SQL Server Connection String


            }

            return RedirectToAction("Import");
        }

I am getting the following Error

Invalid cast from 'System.String' to 'System.Guid'.

My model is below and i cant change Guid as it is necessary need of mine

public class StudentRecord
    {

        public long Id { get; set; }
        public string Name { get; set; }

        public Guid StudentId { get; set; }

    }
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Neeraj Mehta
  • 1,675
  • 2
  • 22
  • 45
  • Not sure whether the ole access to a worksheet will have this, but new OleDbCommand("Select [Id],[Name],Cast(StudentId as UniqueIdentifier) as StudentID .. " might do the job – Tony Hopkinson Mar 18 '14 at 22:07

2 Answers2

1
studentRecord.Guid = new Guid("Mystring");

Maybe, you have to itarate trough the objects what sou got from db, and parse on by one for reach the statement above.

You could also look at using a TypeConverter.

Krekkon
  • 1,349
  • 14
  • 35
0

Parse string to system Guid as below. it may help you

System.Guid.Parse(yourStringVariable);
Vipswelt
  • 345
  • 1
  • 2
  • 6