Getting Error Invalid cast from 'System.String' to 'System.Guid' mysql sql sqlbulkcopy


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))

                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);

                OleDbDataReader dReader;
                dReader = cmd.ExecuteReader();

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

                // 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; }

3/19/2014 11:28:58 PM

Popular Answer

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.

3/18/2014 9:42:09 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow