Click here to Skip to main content
15,917,971 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to save something in memory to file? Pin
Stephen Lintott3-Sep-08 4:08
Stephen Lintott3-Sep-08 4:08 
Generalhelp with webbrowser control C#. net [modified] Solved Pin
Filmxposer2-Sep-08 14:15
Filmxposer2-Sep-08 14:15 
AnswerRe: help with webbrowser control C#. net Pin
Filmxposer2-Sep-08 15:06
Filmxposer2-Sep-08 15:06 
GeneralRe: help with webbrowser control C#. net Pin
#realJSOP2-Sep-08 23:24
professional#realJSOP2-Sep-08 23:24 
QuestionMy Printer CLass Pin
Silvyster2-Sep-08 14:13
Silvyster2-Sep-08 14:13 
AnswerRe: My Printer CLass Pin
JoeSharp2-Sep-08 22:04
JoeSharp2-Sep-08 22:04 
QuestionCrystal Report dataset binding problem Pin
Mudasser Hassan2-Sep-08 13:58
Mudasser Hassan2-Sep-08 13:58 
Questionobject reference not set to an instance of an object??? Pin
Twyce2-Sep-08 12:21
Twyce2-Sep-08 12:21 
I'm using this code(see below)to import data from an excel spreadsheet into my database.now the problem is that it throws "Object reference not set to an instance of an object"exception whenever it gets to this line

Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5,

"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t", false, false, 0, true, true, true);

what am i missing or what am i doing wrong? plz help



public void ImportToStudent()

{

try

{

//Gets the filename, not the path

string filename = ofd.SafeFileName;

string connection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "E:\\..\\PSAtechZTimetableSystem\\SpreadSheet.xls" + "; Extended Properties='Excel 8.0; IMEX=1; HDR=YES'";

// Here is the call to Open a Workbook in Excel

// It uses most of the default values (except for the read-only which we set to true)

Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5,

"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t", false, false, 0, true, true, true);

// get the collection of sheets in the workbook

Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;

// get the first and only worksheet from the collection of worksheets

Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);

using (SqlConnection conPSAtechZ = new SqlConnection(connection))

{

conPSAtechZ.ConnectionString = connection;

using (SqlCommand commandExcel = conPSAtechZ.CreateCommand())

{

System.Data.DataTable table = new System.Data.DataTable();

commandExcel.CommandText = "SELECT [StudentID] AS [StudentID]," + "[ModuleCode] AS [ModuleCode], " + "FROM [Sheet1$]";

//Open the Excel Connection

conPSAtechZ.Open();

using (SqlDataReader dr = commandExcel.ExecuteReader(CommandBehavior.CloseConnection))

{

while (dr.Read() && dr.HasRows)

{

lblResults.Text = "Importing...";

try

{

studentdal.RegisterStudents(new RegisterStudent(Convert.ToString(dr["StudentID"]), Convert.ToString(dr["ModuleCode"])));

commandExcel.CommandType = CommandType.StoredProcedure;

lblResults.Text = "Successfully Imported!!";

}

catch (Exception Ex)

{

MessageBox.Show("Error!!\n " + Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

dr.Close();

}

}

}

}

catch (Exception EX)

{

MessageBox.Show("The spreadsheet is not in the correct format!\n\n" + EX.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

}
AnswerRe: object reference not set to an instance of an object??? Pin
User 66582-Sep-08 12:29
User 66582-Sep-08 12:29 
AnswerRe: object reference not set to an instance of an object??? Pin
MarkB7772-Sep-08 12:53
MarkB7772-Sep-08 12:53 
JokeRe: object reference not set to an instance of an object??? Pin
Dr. Emmett Brown2-Sep-08 12:53
Dr. Emmett Brown2-Sep-08 12:53 
Questionobject reference not set to an instance of an object?????? Pin
Twyce2-Sep-08 12:20
Twyce2-Sep-08 12:20 
AnswerRe: object reference not set to an instance of an object?????? Pin
Mohammad Dayyan2-Sep-08 12:55
Mohammad Dayyan2-Sep-08 12:55 
QuestionHow to copy a Access Database to a SQL Database using C# Pin
three.leaf2-Sep-08 12:03
three.leaf2-Sep-08 12:03 
AnswerRe: How to copy a Access Database to a SQL Database using C# Pin
Dr. Emmett Brown2-Sep-08 12:10
Dr. Emmett Brown2-Sep-08 12:10 
GeneralRe: How to copy a Access Database to a SQL Database using C# Pin
three.leaf2-Sep-08 17:09
three.leaf2-Sep-08 17:09 
QuestionHow to clear MemoryStream ? Pin
Saikek2-Sep-08 11:27
Saikek2-Sep-08 11:27 
AnswerRe: How to clear MemoryStream ? Pin
Bassam Saoud2-Sep-08 11:51
Bassam Saoud2-Sep-08 11:51 
AnswerRe: How to clear MemoryStream ? Pin
Mohammad Dayyan2-Sep-08 13:00
Mohammad Dayyan2-Sep-08 13:00 
GeneralRe: How to clear MemoryStream ? Pin
Saikek2-Sep-08 18:22
Saikek2-Sep-08 18:22 
GeneralRe: How to clear MemoryStream ? Pin
Dave Kreskowiak2-Sep-08 18:41
mveDave Kreskowiak2-Sep-08 18:41 
GeneralRe: How to clear MemoryStream ? Pin
Saikek2-Sep-08 20:31
Saikek2-Sep-08 20:31 
GeneralRe: How to clear MemoryStream ? Pin
Santhosh Thakur12-Oct-11 1:36
Santhosh Thakur12-Oct-11 1:36 
QuestionMy sound stops playing when the form loses focus Pin
DemonicDominic2-Sep-08 10:04
DemonicDominic2-Sep-08 10:04 
AnswerRe: My sound stops playing when the form loses focus Pin
leckey2-Sep-08 10:36
leckey2-Sep-08 10:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.