micah says:
if
(FileUploadImage.HasFile)
{
string
strname = FileUploadImage.FileName.ToString();
FileUploadImage.PostedFile.SaveAs(Server.MapPath(
"~/UserImage/"
) + strname);
string
str = ConfigurationManager.ConnectionStrings[
"conn"
].ConnectionString;
string
getADPOST =
"Insert INTO USERPost (UserName,FriendUserName,SharedUserName,MessageStatus,Symbol,ContentPost,ImageName1) values (@UserName,NULL,NULL,NULL,NULL,@ContentPost,@ImageName1)"
;
using
(SqlConnection con =
new
SqlConnection(str))
{
con.Open();
using
(SqlCommand cmd =
new
SqlCommand(getADPOST, con))
{
cmd.Parameters.AddWithValue(
"@ContentPost"
, TextBixcomment.Text.Trim());
cmd.Parameters.AddWithValue(
"@UserName"
, username);
cmd.Parameters.AddWithValue(
"@FriendUserName"
, username);
cmd.Parameters.AddWithValue(
"@ImageName1"
, strname);
SqlDataAdapter da =
new
SqlDataAdapter(cmd);
DataTable ds =
new
DataTable();
da.Fill(ds);
}
}
}
Change the above code with below code.
string strname = string.Empty;
if (FileUploadImage.HasFile)
{
strname = FileUploadImage.FileName;
FileUploadImage.PostedFile.SaveAs(Server.MapPath("~/UserImage/" + strname));
}
string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
string getADPOST = "Insert INTO USERPost (UserName,FriendUserName,SharedUserName,MessageStatus,Symbol,ContentPost,ImageName1) VALUES (@UserName,NULL,NULL,NULL,NULL,@ContentPost,@ImageName1)";
using (SqlConnection con = new SqlConnection(str))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(getADPOST, con))
{
cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Trim());
cmd.Parameters.AddWithValue("@UserName", username);
cmd.Parameters.AddWithValue("@FriendUserName", username);
cmd.Parameters.AddWithValue("@ImageName1", strname);
cmd.ExecuteNonQuery();
}
}