john kanyora

john kanyora

  • NA
  • 242
  • 46.2k

null reference exemption

Sep 5 2017 10:25 AM
am getting the errror of " object reference not set to an instance of an object" at this line of code:
 
SqlConnection MyCon = new SqlConnection(ConfigurationManager.ConnectionStrings[Excel107ConString].ConnectionString);
 
below is the whole code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8. using System.Data;  
  9. using System.Data.OleDb;  
  10. using System.Data.SqlClient;  
  11. using System.Configuration;  
  12.         public partial class WebForm1 : System.Web.UI.Page  
  13.         {     
  14.             protected void Upload(object sender, EventArgs e)  
  15.             {  
  16.                   
  17.                 string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);  
  18.                 FileUpload1.SaveAs(excelPath);  
  19.   
  20.                 string conString = string.Empty;  
  21.                 string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);  
  22.                 switch (extension)  
  23.                 {  
  24.                      
  25.                     case ".xls":  
  26.                         SqlConnection MyCon = new SqlConnection(ConfigurationManager.ConnectionStrings[Excel107ConString].ConnectionString);  
  27.                        break;  
  28.   
  29.                 }  
  30.                 conString = string.Format(conString, excelPath);  
  31.                 using (OleDbConnection excel_con = new OleDbConnection(conString))  
  32.                 {  
  33.   
  34.                     excel_con.Open();  
  35.                     string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();  
  36.                     DataTable dtExcelData = new DataTable();  
  37.   
  38.   
  39.                     dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("InvoiceNumber"typeof(string)),  
  40.                 new DataColumn("AmountPaid"typeof(decimal)),  
  41.                 new DataColumn("Remarks",typeof(string)) });  
  42.   
  43.                     using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))  
  44.                     {  
  45.                         oda.Fill(dtExcelData);  
  46.                     }  
  47.                     excel_con.Close();  
  48.   
  49.                     string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
  50.                     using (SqlConnection con = new SqlConnection(consString))  
  51.                     {  
  52.                         using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))  
  53.                         {  
  54.                               
  55.                             sqlBulkCopy.DestinationTableName = "smarttable";  
  56.   
  57.                            sqlBulkCopy.ColumnMappings.Add("InvoiceNumber""InvoiceNumber");  
  58.                             sqlBulkCopy.ColumnMappings.Add("AmountPaid""AmountPaid");  
  59.                             sqlBulkCopy.ColumnMappings.Add("Remarks""Remarks");  
  60.                             con.Open();  
  61.                             sqlBulkCopy.WriteToServer(dtExcelData);  
  62.                             con.Close();  
  63.                         }  
  64.                     }  
  65.                 }  
  66.             }  
  67.             
  68. }  

Answers (7)