无法将参数值从String转换为Int32错误

时间:2015-03-03 14:37:54

标签: c# asp.net tsql

我的问题是,在将数据提交到数据库时,我收到错误

  

无法将参数值从String转换为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复选框,但它无效。

4 个答案:

答案 0 :(得分:1)

在该行上看起来像问题;

cmd1.Parameters.Add("@gallery_id", SqlDbType.Int).Value = ddlgallery.SelectedItem.Text;

SelectedItem.Textstring,但您尝试将该值插入到int列,因为您输入的内容为SqlDbType.Int

如果您的列已经输入了一个字符,则需要使用正确的SqlDbType枚举值。但由于您的参数名称为gallery_id,我强烈怀疑它是一个数值。在这种情况下,如果它是有效整数,则需要将ddlgallery.SelectedItem.Text值转换为Int32

同样作为Habib mentioned,您不需要在最后一个参数中使用Convert.ToInt32。由于Checked返回bool,因此SQL Server上的Bit类型正确mapped

并使用using语句为您SqlConnection使用SqlCommand

答案 1 :(得分:1)

您是否尝试过使用Convert.ToInt32(ddlgallery.SelectedItem.Text);而不是直接传递文字?

答案 2 :(得分:0)

ddlgallery.SelectedItem.Text给您带来了麻烦。

尝试ddlgallery.SelectedItem.Value,即使它出现问题,请尝试

Convert.ToInt32(ddlgallery.SelectedItem.Value)

通常dropdown lists在前端显示文字,但它们有一个与之关联的整数值,可以使用ddlgallery.SelectedItem.Value

提取

此致

答案 3 :(得分:0)

实际上问题不在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);
            }

        }
    }