When i try to submit text without image or video upload it will show this error below
Server Error in '/' Application.
ExecuteNonQuery: CommandText property has not been initialized
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: ExecuteNonQuery: CommandText property has not been initialized
Source Error:
Line 390: cmd.Connection = con;
Line 391: con.Open();
Line 392: cmd.ExecuteNonQuery();
Line 393: con.Close();
Line 394: string message = "Post submitted successfully.";
C SHARP CODE
protected void LinkBPOST_Click(object sender, EventArgs e)
{
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetRandomFileName());
string folderPath = Server.MapPath("~/path/"); //Please enter your folder Name here.
string ImagefolderPath = Server.MapPath("~/UserImage/"); //Please enter your folder Name here.
if (this.Page.User.Identity.IsAuthenticated)
{
string username = this.Page.User.Identity.Name;
{
string tendString = TextBixcomment.Text.Trim();
string strname = "";
string extension = "";
string getADPOST = "";
string str = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
if (FileUploadpost.HasFile || TextBixcomment.Text != "")
{
strname = FileUploadpost.FileName.ToString();
extension = System.IO.Path.GetExtension(strname);
using (SqlConnection con = new SqlConnection(str))
{
SqlCommand cmd = new SqlCommand();
// Image extension.
if (extension.ToLower() == ".jpg" || extension.ToLower() == ".png" || extension.ToLower() == ".gif" || extension.ToLower() == ".jpeg")
{
getADPOST = "Insert INTO USERPost (UserName,ContentPost,ImageName1) values (@UserName,@ContentPost,@ImageName1)";
cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Replace(Environment.NewLine, "<br/>").Trim());
cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
cmd.Parameters.AddWithValue("@ImageName1", fileNameWithoutExtension + extension);
FileUploadpost.SaveAs(ImagefolderPath + fileNameWithoutExtension + extension); //save file from fileUpload to folder here.
}
// Video extension.
else if (extension.ToLower() == ".mp4" || extension.ToLower() == ".wmv" || extension.ToLower() == ".flv" || extension.ToLower() == ".vob" || extension.ToLower() == ".dat")
{
getADPOST = "Insert INTO USERPost (UserName,ContentPost,Path) values (@UserName,@ContentPost,@Path)";
cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Replace(Environment.NewLine, "<br/>").Trim());
cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
cmd.Parameters.AddWithValue("@Path", "path/" + fileNameWithoutExtension + extension);
FileUploadpost.SaveAs(folderPath + fileNameWithoutExtension + extension); //save file from fileUpload to folder here.
}
cmd.CommandText = getADPOST;
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
string message = "Post submitted successfully.";
ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openModal5('" + message + "');", true);
}
}
else
{
string message = "Please Enter the comment or select any Video or Image file!";
string script = "window.onload = function(){ alert('";
script += message;
script += "')};";
Page.ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
}
}
}
this.Populatebooks();
}