无法将参数值从 String 转换为 Int32 错误
Failed to convert parameter value from a String to a Int32 error
我的问题是在将数据提交到数据库时出现错误
Failed to convert parameter value from a String to a 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
,但它不起作用。
该行似乎有问题;
cmd1.Parameters.Add("@gallery_id", SqlDbType.Int).Value = ddlgallery.SelectedItem.Text;
SelectedItem.Text
是 string
但您尝试将该值插入到 int 列中,因为您输入的是 SqlDbType.Int
.
如果您的列已经 键入了一个字符,您需要使用正确的SqlDbType
枚举值。但是由于你的参数名称gallery_id
,我强烈怀疑它是一个数值。在这种情况下,如果 ddlgallery.SelectedItem.Text
值是有效整数,则需要将其转换为 Int32
。
也作为 Habib , you don't need to use Convert.ToInt32
in your last parameter. Since Checked
returns bool
, it is correctly mapped 在 SQL 服务器上使用 Bit
键入。
并像 SqlCommand
一样对 SqlConnection
使用 using
语句。
您是否尝试过使用 Convert.ToInt32(ddlgallery.SelectedItem.Text);
而不是直接传递文本?
ddlgallery.SelectedItem.Text
给您带来了麻烦。
尝试ddlgallery.SelectedItem.Value
,即使它给你带来问题,然后尝试
Convert.ToInt32(ddlgallery.SelectedItem.Value)
.
通常dropdown lists
在前端显示文本,但它们有一个与之关联的整数值,可以使用ddlgallery.SelectedItem.Value
提取
此致
其实问题不在 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);
}
}
}
我的问题是在将数据提交到数据库时出现错误
Failed to convert parameter value from a String to a 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
,但它不起作用。
该行似乎有问题;
cmd1.Parameters.Add("@gallery_id", SqlDbType.Int).Value = ddlgallery.SelectedItem.Text;
SelectedItem.Text
是 string
但您尝试将该值插入到 int 列中,因为您输入的是 SqlDbType.Int
.
如果您的列已经 键入了一个字符,您需要使用正确的SqlDbType
枚举值。但是由于你的参数名称gallery_id
,我强烈怀疑它是一个数值。在这种情况下,如果 ddlgallery.SelectedItem.Text
值是有效整数,则需要将其转换为 Int32
。
也作为 Habib Convert.ToInt32
in your last parameter. Since Checked
returns bool
, it is correctly mapped 在 SQL 服务器上使用 Bit
键入。
并像 SqlCommand
一样对 SqlConnection
使用 using
语句。
您是否尝试过使用 Convert.ToInt32(ddlgallery.SelectedItem.Text);
而不是直接传递文本?
ddlgallery.SelectedItem.Text
给您带来了麻烦。
尝试ddlgallery.SelectedItem.Value
,即使它给你带来问题,然后尝试
Convert.ToInt32(ddlgallery.SelectedItem.Value)
.
通常dropdown lists
在前端显示文本,但它们有一个与之关联的整数值,可以使用ddlgallery.SelectedItem.Value
此致
其实问题不在 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);
}
}
}