Click here to Skip to main content
14,366,974 members
Rate this:
Please Sign up or sign in to vote.
I am getting this error Object reference not set to an instance of an object. when I run a form which shows the Crystal Report after button click.
My Button Click Event code is:
Data_Conn DC = new Data_Conn(Application.StartupPath);
            button1.Enabled = false;
            button1.Text = "Openning..";
           
            try
            { 
                Application.DoEvents();
                if (checkBox1.Checked)
                {
                    DC.SelectSta("select comName,iType,iName,(qty & ' ' &  unit) as qtyU,mrp,sealBscVATrs,unitPri from StockDetails sd,ItemsTbl it where it.iCode=sd.iCode;", "StockDetails");
                }               
                else
                {                   
                    button1.Text = "Report";
                    button1.Enabled = true;
                    return;
                }
                if (DC.getDataSet.Tables["StockDetails"].Rows.Count > 0)
                {
                    //MessageBox.Show("Working");

                    StockDataSet SDS = new StockDataSet();
                    DC.getAdapter.Fill(SDS);
                    StockReport StR = new StockReport();
                    StR.SetDataSource(SDS.Tables[1]);
                    StR.Refresh();
                    showReport SR = new showReport();
                    SR.crystalReportViewer1.ReportSource = StR;
                    SR.crystalReportViewer1.Refresh();
                    
SR.MdiParent = this.MdiParent;
                    SR.Show();                    
                }
                else
                {
                    MessageBox.Show("Items not Found!!", "Winform App", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

The interesting thing is, when I run in debugging mode(F11) then it will show the records in report(no error), but when I run without debugging mode(F5) then it will give that error...
the error show the code which in Program.cs file :
Application.Run(new Form1());

the source of this error is: CrystalDecisions.Windows.Forms(I checked it from view details).

Please Help me..

Thanks in advanced...
Regards
Jayanta.
Posted
Comments
nicola_melc 1-Dec-14 8:37am
   
If you put your code into try-catch you can show the error. Like this:

try
{

}
catch (Exception a)
{
MessageBox.Show(a.Message.ToString());
}

At the moment I have the same problem with my application.
Probabily when you did:

if (DC.getDataSet.Tables["StockDetails"].Rows.Count > 0)

It continually to count your rows also after the last.
   
Basically, you are right, but 1) Message would not be informative enough; 2) the catch block may miss the origin of the exception. OP would need to see the exception call stack.
I provided more comprehensive instructions in Solution 1; please see.
—SA
JayantaChatterjee 1-Dec-14 9:19am
   
I used try catch, but the exception doesn't caught by the catch statement.. :-(
so what is the solution for this "rows count" ??
what you did?
nicola_melc 1-Dec-14 10:21am
   
I've solved using a SqlDataReader.

This is my code:

string SQLquery = "SELECT * FROM dbo.ripRiparazioni";

SqlDataAdapter da = new SqlDataAdapter(SQLquery, connessione);

DataSet ds = new DataSet();

da.Fill(ds);

dgvListaRiparazioni.DataSource = ds.Tables[0];

SqlCommand command = new SqlCommand(SQLquery, connessione);

SqlDataReader reader = command.ExecuteReader();

foreach (DataGridViewRow righe in dgvListaRiparazioni.Rows)
{
if (reader.HasRows)
{
while (reader.Read())
{
DateTime date = (DateTime)righe.Cells["Data"].Value;
if (date.Date >= DateTime.Now.AddDays(-3).Date)
{
righe.DefaultCellStyle.BackColor = Color.White;
}
else
{
righe.DefaultCellStyle.BackColor = Color.Yellow;
}
}
}

}

Hope it is helpful.
JayantaChatterjee 1-Dec-14 10:34am
   
thanks a lotttttt..
nicola_melc 1-Dec-14 10:40am
   
But, you used a DataGridView?
JayantaChatterjee 1-Dec-14 22:39pm
   
No, I didn't use DataGridView..
I trying to get all the records from the table for display in "Crystal Report"..
Rate this:
Please Sign up or sign in to vote.

Solution 1

You did show where the exception with the message "Object reference not set to an instance of an object" is thrown, but you did not provide all the detail. Fortunately, the authors of other answers were able to point it out. But you cannot ask this question every time it happens, need, to learn how to sort it out by yourself.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
   
v3
Comments
Maciej Los 1-Dec-14 11:01am
   
5ed!
   
Thank you, Maciej.
—SA
BillWoodruff 2-Dec-14 1:41am
   
+5 imho this is you, as a teacher, at your very best !
   
Wow...
Thank you, Bill; I wish some would really learn from these instructions... :-)
—SA
JayantaChatterjee 3-Dec-14 7:05am
   
Sorry for late reply..
thank you sir, I will keep it in mind..
   
You are welcome. Late reply is never a problem (and is never late), please don't worry about such things.
Will you accept the answer formally?
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 3

Basically this errors com when you want to access the field or value but that value does not exist.
Suppose you have a dropdownlist in which you have 3 options short,middle,long but you want to access shorter(that is not available in dropdownlist).
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100