Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# object instance on
Why i getting error : Error! Object reference not set on instance of an object. c#
 
private StringBuilder htmlMessageBody(DataGridView dg)
{
    StringBuilder strB = new StringBuilder();
    //create html & table
    strB.AppendLine("<html><body><center><table border='1' cellpadding='0' cellspacing='0'>");
    strB.AppendLine("<tr>");
    //cteate table header
    for (int i = 0; i < dg.Columns.Count; i++)
    {
        strB.AppendLine("<td align='center' valign='middle'>" + dg.Columns[i].HeaderText + "</td>");
    }
    //create table body
    strB.AppendLine("<tr>");
    for (int i = 0; i < dg.Rows.Count; i++)
    {
        strB.AppendLine("<tr>");
        foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
        {
            strB.AppendLine("<td align='center' valign='middle'>" + dgvc.Value.ToString() + "</td>");
        }
        strB.AppendLine("</tr>");
 
    }
    //table footer & end of html file
    strB.AppendLine("</table></center></body></html>");
    return strB;
}
 

 

private void bMail_Click(object sender, EventArgs e)
{
 
    MailMessage myMessage = new MailMessage();
    try
    {
        myMessage.From = "test@test.com";
        myMessage.To = "test@test.com";
        myMessage.Cc = "test@test.com";
        myMessage.BodyEncoding = Encoding.UTF8;
        myMessage.BodyFormat = MailFormat.Html;
        myMessage.Body = htmlMessageBody(dataGridView2).ToString();
        myMessage.Subject = "Test";
 
        myMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "0");
        myMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "userName");
        myMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
        SmtpMail.SmtpServer = "mail.t-com.hr";
        SmtpMail.Send(myMessage);
        MessageBox.Show("Message sent!");
 
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error! " + ex.Message);
    }
Posted 16-Apr-13 4:15am
ant 385275
Comments
Marcus Kramer at 16-Apr-13 9:17am
   
On what line of code is the error happening. You need to use your debugger and step through the code at which point it will at least tell you what line. Tell us that and then we can help you.
Member 10381340 at 12-Nov-13 7:01am
   
public void LoadCombo()
{
con = new Connection("Cartridges");
foreach (DataRow row in con.DataS.Tables["Cartridges"].Rows)
{
cboCartridgeCode.Items.Add(row["CartridgeCode"].ToString());
}
con.CloseCon();
}
 
Object reference not set to an instance of an object... is on the foreach loop (foreach keyword is highlighted), plz help
Pheonyx at 16-Apr-13 9:33am
   
When do you instantiate SmtpMail ? from the code you have shown it doesn't appear to be defined/instantiated, just used.
babu saravanan at 17-Apr-13 0:10am
   
can you please give ti in more detail where error occured
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

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"[^].
 
Good luck,
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Can't get the source above to compile but I did notice the following :
 
To send the mail, you need to do something like this :
 
SmtpClient client = new SmtpClient("mail.t-com.hr");
client.Send(myMessage);
 
I suspect that your exception may be coming from the dataGridView2 passed into the htmlMessageBody method. Step through the code and make sure that the variable 'dg' is not null when it enters that method.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 341
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 220
3 Praneet Nadkar 197
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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