2014年7月13日 星期日

[RESOLVED] Object reference not set to an instance of an object.


Hi,


I am trying to use upload option for attachment, there is no error but this one Object reference not set to an instance of an object. when every I try to run the page.


This is the code :



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



public partial class sendquote : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["dstrConnectionString"].ConnectionString;
string str;
SqlCommand com;

protected void Page_Load(object sender, EventArgs e)
{

insertdata();

}

public void insertdata()
{

SqlConnection con = new SqlConnection(strConnString);

con.Open();
int len = Attachment1.PostedFile.ContentLength;
int len2 = Attachment2.PostedFile.ContentLength;
byte[] pic = new byte[len + 1];
byte[] pic2 = new byte[len2 + 1];

Attachment1.PostedFile.InputStream.Read(pic, 0, len);
str = "insert into data values (@Attachment1,@Attachment2)";
com = new SqlCommand(str, con);

com.Parameters.AddWithValue("@Attachment1", pic);
com.Parameters.AddWithValue("@Attachment2", pic2);

com.ExecuteNonQuery();
con.Close();
lblMessage.Text = " Details Sent Successfully!";

Response.Redirect("employee.aspx");


con.Close();




}
}

When I run the page, It point me to int len = Attachment1.PostedFile.ContentLength;  

with the error Object reference not set to an instance of an object.


Can anyone help me in this please to sort it out.



Hi buddy,

remove your call from pageload, put it in a button event, here is your code:




public partial class sendquote : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["dstrConnectionString"].ConnectionString;
SqlCommand com = null;
string str = "";

protected void Page_Load(object sender, EventArgs e)
{

}

public void insertdata()
{
try
{
SqlConnection con = new SqlConnection(strConnString);

if (Attachment1.HasFile && Attachment2.HasFile)
{
con.Open();
int len = Attachment1.PostedFile.ContentLength;
int len2 = Attachment2.PostedFile.ContentLength;
byte[] pic = new byte[len + 1];
byte[] pic2 = new byte[len2 + 1];

Attachment1.PostedFile.InputStream.Read(pic, 0, len);
str = "insert into data values (@Attachment1,@Attachment2)";
com = new SqlCommand(str, con);

com.Parameters.AddWithValue("@Attachment1", pic);
com.Parameters.AddWithValue("@Attachment2", pic2);

com.ExecuteNonQuery();
con.Close();
lblMessage.Text = "Details Sent Successfully!";

Response.Redirect("employee.aspx");
con.Close();
}
else
{
lblMessage.Text = "No files selected!";
}
}
catch (Exception ex)
{
//TODO
}
}

protected void btn_upload_Click(object sender, EventArgs e)
{
insertdata();
}
}







Use insertdata() in any button event.





sanjay bathre



Hi buddy,

remove your call from pageload, put it in a button event, here is your code:




public partial class sendquote : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["dstrConnectionString"].ConnectionString;
SqlCommand com = null;
string str = "";

protected void Page_Load(object sender, EventArgs e)
{

}

public void insertdata()
{
try
{
SqlConnection con = new SqlConnection(strConnString);

if (Attachment1.HasFile && Attachment2.HasFile)
{
con.Open();
int len = Attachment1.PostedFile.ContentLength;
int len2 = Attachment2.PostedFile.ContentLength;
byte[] pic = new byte[len + 1];
byte[] pic2 = new byte[len2 + 1];

Attachment1.PostedFile.InputStream.Read(pic, 0, len);
str = "insert into data values (@Attachment1,@Attachment2)";
com = new SqlCommand(str, con);

com.Parameters.AddWithValue("@Attachment1", pic);
com.Parameters.AddWithValue("@Attachment2", pic2);

com.ExecuteNonQuery();
con.Close();
lblMessage.Text = "Details Sent Successfully!";

Response.Redirect("employee.aspx");
con.Close();
}
else
{
lblMessage.Text = "No files selected!";
}
}
catch (Exception ex)
{
//TODO
}
}

protected void btn_upload_Click(object sender, EventArgs e)
{
insertdata();
}
}




I tried it and it work. Just wanted to confirm. If I want a condition where either attachment1 box is uploaded or attachment2 box is uploaded and proceed the code .


I have used this


 if (Attachment1.HasFile || Attachment2.HasFile)

Is that ok ?




Hello New2World,


Greetings , Hope you doing fine,


I have modified your code a bit for better utilisation of your connection.And yes you can proceed with an OR condition:


public void insertdata()
{
try
{
if (Attachment1.HasFile || Attachment2.HasFile)
{
using(SqlConnection con = new SqlConnection(strConnString))
{
con.Open();
int len = Attachment1.PostedFile.ContentLength;
int len2 = Attachment2.PostedFile.ContentLength;
byte[] pic = new byte[len + 1];
byte[] pic2 = new byte[len2 + 1];

Attachment1.PostedFile.InputStream.Read(pic, 0, len);
str = "insert into data values (@Attachment1,@Attachment2)";
com = new SqlCommand(str, con);

com.Parameters.AddWithValue("@Attachment1", pic);
com.Parameters.AddWithValue("@Attachment2", pic2);

com.ExecuteNonQuery();
// no need to close the connection , using keyword will dispose the same.
//con.Close();
// No need to display a message you wont be able to see it. :)
//lblMessage.Text = "Details Sent Successfully!";
//con.Close();
}
Response.Redirect("employee.aspx");
}
else
{
lblMessage.Text = "No files selected!";
}
}
catch (Exception ex)
{
//Dont leave catch block empty
lblError.Text="Opss ! something went wrong , please try again later";
}
}

Please feel free to ask any question(s).


Hope This Helps.


Have a great day.


沒有留言:

張貼留言