Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void btnFinish_Click(object sender, EventArgs e)
{
Panel6.Visible = true;
string fileType = filePhoto.PostedFile.ContentType;
//Response.Write(fileType);
if (fileType == "image/jpeg " || fileType == "image/jpeg" || fileType == "image/gif" || fileType == "image/png" || fileType == "image/bmp")
{
string fileName = System.IO.Path.GetFileName(filePhoto.PostedFile.FileName);

string serverFolderPath = Server.MapPath("Photos\\");
if (!Directory.Exists(serverFolderPath))
Directory.CreateDirectory(serverFolderPath);
string serverFileName = serverFolderPath + fileName;
filePhoto.SaveAs(serverFileName);
lblPhoto.Text = "File Uploaded successfully ";

}
else
{

lblPhoto.Text = " Please Select file of type *.jpeg/*.jpg/*.png/*.gif/*.bmp only!!!";
}
ViewState["Pic"] = "Photos/" + filePhoto.PostedFile.FileName;
string photoPath =Convert.ToString(ViewState["Pic"]);
lblDetails.Text += "
<img width='150px' height='100px' src='" + photoPath + "'>";
}

protected void btnConfirm_Click(object sender, EventArgs e)
{
string photo = Convert.ToString(ViewState["Pic"]);


// photo = ViewState["Pic"].ToString();
Label1.Text = photo;
if (cn.State != ConnectionState.Open)
cn.Open();

strSqlCommand = "instert into demo(id,img) values(1," + photo + ")";
int rowAffected = cmd.ExecuteNonQuery();
if (rowAffected > 0)
{
Label1.Text += " inserted ";
//Label1.Text += " "+image;
}
else
Label1.Text = "not done ";
}
Posted

1 solution

The only way you will get a null reference there is to not assign anything to the cmd - and your code doesn't.

You create a command string:
C#
strSqlCommand = "instert into demo(id,img) values(1," + photo + ")";
but you don't do anything with it. (and you can't spell "insert" :laugh: )
So try:
C#
strSqlCommand = "INSERT INTO demo (id,img) values(1," + photo + ")";
cmd = new SqlCommand(strSqlCommand, con);
int rowAffected = cmd.ExecuteNonQuery();

But please, don't do it that way! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:
C#
strSqlCommand = "INSERT INTO demo (id,img) values(1,@PH)";
cmd = new SqlCommand(strSqlCommand, con);
cmd.Parameters.AddWithValue("@PH", photo);
int rowAffected = cmd.ExecuteNonQuery();
 
Share this answer
 
Comments
Bhavani Ch 29-Mar-15 7:16am    
Thank you for information :)
OriginalGriff 29-Mar-15 7:21am    
You're welcome!

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