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 !";
}

Rajeesh MenothRank #118 · 18.4K reputationPosted Dec 18, 2016, 8:15 AM EST
Manas MohapatraRank #182 · 13.2K reputationPosted Dec 18, 2016, 8:56 AM EST
Amit Kumar SinghRank #562 · 4.3K reputationPosted Dec 18, 2016, 9:16 AM EST
{
string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
using(SqlConnection connection = new SqlConnection(connectionInfo))
{
connection.Open();
// perform work with connection
}
}
IsraelRank #1,223 · 1.5K reputationPosted Dec 18, 2016, 9:52 AM EST
Mangesh GRank #294 · 8.6K reputationPosted Dec 18, 2016, 12:07 PM EST
Manas MohapatraRank #182 · 13.2K reputationPosted Dec 18, 2016, 8:40 PM EST