I have a gridview where I have to select images via FileUpload and submit the button click so the path and related information gets submitted into the database.
But When I keep the gridview empty and click on submit button I get error as
Must declare the scalar variable "@img_path".
Is there any way by which I can provide validations so that user has to select the images and then only submit the form
Here is my code below:-
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString);
foreach (GridViewRow row in ImagesGrid.Rows)
{
var title = row.FindControl("txtTitle") as TextBox;
var description = row.FindControl("txtDescription") as TextBox;
var imageFile = row.FindControl("flUpload") as FileUpload;
var chkDefault = row.FindControl("chkDefault") as CheckBox;
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
SqlCommand cmd1 = new SqlCommand("Insert into tbl_galleries_stack (gallery_id,img_title,img_desc,img_path,IsDefault) values (@gallery_id,@img_title,@img_desc,@img_path,@IsDefault)", conn);
cmd1.Parameters.Add("@gallery_id", SqlDbType.Int).Value = Convert.ToInt32(ddlgallery.SelectedValue);
cmd1.Parameters.Add("@img_title", SqlDbType.NVarChar).Value = title.Text;
cmd1.Parameters.Add("@img_desc", SqlDbType.NVarChar).Value = description.Text;
if (imageFile.HasFile)
{
imageFile.SaveAs(Server.MapPath("~/GalleryImages/") + imageFile.FileName);
cmd1.Parameters.Add("@img_path", SqlDbType.NVarChar).Value = "~/GalleryImages/" + imageFile.FileName;
}
cmd1.Parameters.Add("@IsDefault", SqlDbType.Bit).Value = chkDefault.Checked;
cmd1.ExecuteNonQuery();
conn.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Gallery images added sucessfully');window.location ='csrgalleriesstack.aspx';", true);
}
}
}
Please suggest what to do
Aucun commentaire:
Enregistrer un commentaire