.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

What is the meaning of this error :--Object reference not set to an instance of an object.

Posted By: teja     Posted Date: March 07, 2013    Points:5   Category :ASP.Net
Hi,I was create a page,that is Uploading data files into sql server.
when i execute this page,am getting error like ----

Error uploading Document: Object reference not set to an instance of an object.
what is the meaning of this error.how should i clear that error?
and my code is


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.IO;



namespace web
{
public partial class _new : System.Web.UI.Page
{
public SqlConnection objConn;
public SqlCommand objCmd;
public String strConn;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void cmdUpload_Click(object sender, EventArgs e)
{
try
{
if (fileUploadDocument.PostedFile.ContentLength > 0)
{
// Get the File name and Extension
string strFileName = Path.GetFileName(fileUploadDocument.PostedFile.FileName);
string strFileExtension = Path.GetExtension(fileUploadDocument.PostedFile.FileName);
// Extract the content of the Document into a Byte array
int intlength = fileUploadDocument.PostedFile.ContentLength;
Byte[] byteData = new Byte[intlength];
fileUploadDocument.PostedFile.InputStream.Read(byteData, 0, intlength);
// Save the file to the DB
strConn = ConfigurationManager.ConnectionStrings["EmployeesConnectionString"].ToString();
objConn = new SqlConnection(strConn);
objCmd = new SqlCommand("InsertDocument", objConn);
objCmd.CommandType = CommandType.StoredProcedure;
SqlParameter pFileName = new SqlParameter("FileName", SqlDbType.VarChar);
pFileName.Size = 50;
pFileName.Direction = ParameterDirection.Input;
pFileName.Value = strFileName;
SqlParameter pExtension = new SqlParameter("Extension", SqlDbType.VarChar);
pExtension.Size = 5;
pExtension.Direction = ParameterDirection.Input;
pExtension.Value = strFileExtension;
//
SqlParameter pFileContent = new SqlParameter("FileContent", SqlDbType.VarBinary);
pFileContent.Size = byteData.Length;
pFileContent.Direction = ParameterDirection.Input;
pFileContent.Value = byteData;
//
objCmd.Parameters.Add(pFileName);
objCmd.Parameters.Add(pExtension);
objCmd.Parameters.Add(pFileContent);
//
objConn.Open();
objCmd.ExecuteNonQuery();
objConn.Close();
//
lblMsg.Text = "Document Uploaded Succesfully";
}
}
catch (Exception ex)
{
lblMsg.Text = " Error uploading Document: " + ex.Message.ToString();
}
}
}
}

Responses
Author: govinda alwani             
Posted Date: March 08, 2013     Points: 20   

When you have just declare the object, and you are assigning value to it then this error occurs "Object reference not set to an instance of an object"

let take example


Error.................
ClassA objA;
objA = SomeValue;

Right way.............
ClassA objA;
objA = new ClassA();
objA = someValue;

just correct this line and let me know is this was problem.?
public String strConn="";

Author: teja             
Posted Date: March 08, 2013     Points: 20   

hi govinda alwani..
I am getting error while using public String strConn="".
and classA ObjA;type's are not working,insted of this i used
public SqlConnection objConn;
public SqlCommand objCmd;
public String strConn;

so can you please clarify my doubt?i am not getting

Author: SailajaReddy             
Posted Date: March 08, 2013     Points: 20   

Hi Teja,

Syntax wise ur code is fine.
On uploading document , it is giving error , means there is some error occuring in the try block.
either connection string or Stored procedure u r calling or parameters of that SP .
Check everything once and try to put a break point inside the function "cmdUpload_Click" and run it using F10 , then it will show where the error is.

If u r not able to solve , tell ur requirements and someone could provide u the working code :-)

Author: teja             
Posted Date: March 08, 2013     Points: 20   

Thank you,sailajareddy.
Can you please clarify my another doubt?
My doubt is,i want to create a online examination paper,so i think that,i need to create a html page which is used to create 30 questions,and each and every question having 4 multiple options with RadioButtons.so i want to upload this html file into sql database and retrieve it from the sql for examination.and if we retrieve the HTML file(Question paper) from database,can we calculate the marks for each question?please clear my doubt,can we upload and retrieve the file from database using fileupload control?please suggest me.




Thank You

Author: SailajaReddy             
Posted Date: March 08, 2013     Points: 20   

Hi Teja,

I dont think we can do this with upload file. As we need to store the answers marked by student.
Online exam .. 2 types
1)All 30 questions in a single page
2)one question in each page.

For the first scenario , if all 30 ques in a page , then take aspx page , take grid view , in each row add 1 question , 4 radio buttons. At the end add submit , cancel buttons.

For the second scenario , for 30 questions , create 30 pages. in each page , write question and add 4 radio buttons for multiple choice. provide next and prev buttons here.

So if u design like this , it would be easy to store the answers selected by the student.
and finally u can display the marks obtained also.

Thanks.

Author: teja             
Posted Date: March 10, 2013     Points: 20   

Hi,
Thank you for your reply.If you don't mind,can you please tell me the code for this,and how to calculate the marks automatically?

Author: teja             
Posted Date: March 12, 2013     Points: 20   

Hi,can you please tell me how to add questions in each row,please help me i am new to this .net



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend