我的问题是,在将数据提交到数据库时,出现错误
无法将参数值从字符串转换为Int32。
这是我的代码:-
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;
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 = ddlgallery.SelectedItem.Text;
cmd1.Parameters.Add("@img_title", SqlDbType.VarChar).Value = title;
cmd1.Parameters.Add("@img_desc", SqlDbType.VarChar).Value = description;
cmd1.Parameters.Add("@img_path", SqlDbType.NVarChar).Value = imageFile;
cmd1.Parameters.Add("@IsDefault", SqlDbType.Bit).Value = Convert.ToInt32(chkDefault.Checked);
cmd1.ExecuteNonQuery();
conn.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Teachers profile added sucessfully');window.location ='csrgalleriesstack.aspx';", true);
}
}
}
我尝试更改Int32
为复选框,但无法正常工作。
其实问题不在gallery_id
,问题是,我必须filename
随路径发送。像这样
cmd1.Parameters.Add("@img_path", SqlDbType.NVarChar).Value = "~/Images/" + imageFile.FileName;
那对我有用。
所以我的最终代码如下:
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('Teachers profile added sucessfully');window.location ='csrgalleriesstack.aspx';", true);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句