Israel

Israel

  • 699
  • 1.3k
  • 217.3k

Object reference not set to an instance of an object(???)

Dec 18 2016 11:26 AM
Hi!
 
I am testing my code. Curiously, later after running I have this message (Object reference not set to an instance of an object) pointed on this line here:
string connStr = ConfigurationManager.AppSettings["Data Source=LUNASOFTRESEARC\\SQLEXPRESS;Initial Catalog=RentMyWrox;Integrated Security=True"].ToString(); 
I need your help please. Thank you!
 
string fileName = FileUpload1.PostedFile.FileName;
int fileLength = FileUpload1.PostedFile.ContentLength;
byte[] imageBytes = new byte[fileLength];
FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, fileLength);
string connStr = ConfigurationManager.AppSettings["Data Source=LUNASOFTRESEARC\\SQLEXPRESS;Initial Catalog=RentMyWrox;Integrated Security=True"].ToString(); //Here where is the bugg
using (SqlConnection conn = new SqlConnection(connStr))
{
string sql = "INSERT INTO ImageUpload (PictureName, PictureFile) VALUES (@pictureName, @pictureFile)";
SqlParameter[] prms = new SqlParameter[2];
prms[0] = new SqlParameter("@pictureName", SqlDbType.VarChar, 50);
prms[0].Value = fileName;
prms[1] = new SqlParameter("@pictureFile", SqlDbType.Image);
prms[1].Value = imageBytes;
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(prms);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
lblMessage.Text = "Picture uploaded successsfully !";
}
 
 

Answers (6)