Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why i getting error : Error! Object reference not set on instance of an object. c#

XML
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
Comments
fjdiewornncalwe 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 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 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.
[no name] 17-Apr-13 0:10am    
can you please give ti in more detail where error occured

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.
 
Share this answer
 
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
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900