简体   繁体   English

无法将参数值从字符串转换为Int32错误

[英]Failed to convert parameter value from a String to a Int32 error

My issue is that while submitting the data into the database I am getting error as 我的问题是,在将数据提交到数据库时,由于

Failed to convert parameter value from a String to a Int32. 无法将参数值从字符串转换为Int32。

Here is my code:- 这是我的代码:

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);
            }

        }
}

I tried changing to Int32 for the checkbox but it is not working. 我尝试将复选框更改为Int32 ,但无法正常工作。

Looks like problem on that line; 看起来像那条线上的问题;

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

SelectedItem.Text is string but you try to insert that value to an int column since you typed as SqlDbType.Int . SelectedItem.Textstring但由于键入为SqlDbType.Int ,因此您尝试将其插入int列。

If your column is already a character typed, you need to use the right SqlDbType enum value. 如果您的列已经是一个字符类型,则需要使用正确的SqlDbType枚举值。 But since your parameter name gallery_id , I strongly suspect it is a numeric value. 但是由于您的参数名称gallery_id ,我强烈怀疑它是一个数字值。 In such a case, you need to convert that ddlgallery.SelectedItem.Text value to Int32 if it is a valid integer. 在这种情况下,如果该ddlgallery.SelectedItem.Text值是有效整数,则需要将其转换为Int32

Also as Habib mentioned , you don't need to use Convert.ToInt32 in your last parameter. 就像Habib 提到的那样 ,您不需要在最后一个参数中使用Convert.ToInt32 Since Checked returns bool , it is correctly mapped with Bit type on SQL Server. 由于Checked返回bool ,因此在SQL Server上已正确将其与Bit类型进行了映射

And use using statement for your SqlConnection as you did for SqlCommand . 像对SqlCommand一样,对SqlConnection使用using语句。

Have you tried using Convert.ToInt32(ddlgallery.SelectedItem.Text); 您是否尝试过使用Convert.ToInt32(ddlgallery.SelectedItem.Text); instead of passing Text directly? 而不是直接传递文本?

ddlgallery.SelectedItem.Text is causing problem for you. ddlgallery.SelectedItem.Text为您造成问题。

Try ddlgallery.SelectedItem.Value , even if it gives you a problem, then try 尝试ddlgallery.SelectedItem.Value ,即使它给您带来了问题,也请尝试

Convert.ToInt32(ddlgallery.SelectedItem.Value) . Convert.ToInt32(ddlgallery.SelectedItem.Value)

Usually dropdown lists show text in the front end, but they have an integer value associated with them, which can be extracted using ddlgallery.SelectedItem.Value 通常, dropdown lists在前端显示文本,但它们具有与之关联的整数值,可以使用ddlgallery.SelectedItem.Value进行提取。

Regards 问候

Actually the issue was not in the gallery_id , The issue was, I had to send the filename with the path. 实际上,问题不在gallery_id ,问题在于,我必须发送filename和路径。 like this 像这样

cmd1.Parameters.Add("@img_path", SqlDbType.NVarChar).Value = "~/Images/" + imageFile.FileName;

And that worked for me. 那对我有用。

So my final code was like 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('Teachers profile added sucessfully');window.location ='csrgalleriesstack.aspx';", true);
            }

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM