插入数据库时​​出现ASP.NET错误

时间:2021-08-31 15:17:41

I get error while insert data into the database. I use ASP.NET

将数据插入数据库时​​出错。我使用ASP.NET

My C# code is:

我的C#代码是:

 protected void btn_save_coins_Click(object sender, EventArgs e)
    {

        using (SqlConnection con = new SqlConnection(constr)) // to link between conection string and database
        {
            try
            {

                SqlCommand cmd = new SqlCommand("insert into Coins (Title,Title_ar,country,value,face_value, Year,Era,headofstate,"+
                "Designer, Theme,Material,Weight,Diameter,Thickness, Punch,Shape,Obverse,Reverse,Edge,Privy_mark,Mint_mark,Details,Qty ) "+
                "values(@T,@TAr,@c,@v,@fv,@y,@e,@h,@d,@th,@m,@w,@di,@thi,@p,@s,@o,@r,@ed,@pr,@mi,@de,@Q)", con);
                                //id
                cmd.Parameters.AddWithValue("@T", txt_Title.Text.ToString());
                cmd.Parameters.AddWithValue("@TAr", txt_TitleAr.Text.ToString());
                cmd.Parameters.AddWithValue("@c", Convert.ToInt32(Session["c"]));                   //country
                cmd.Parameters.AddWithValue("@v", txt_value.Text.Trim());
                cmd.Parameters.AddWithValue("@fv", ddl_Face_value.DataValueField.Trim());
                cmd.Parameters.AddWithValue("@y", txt_Year.Text.Trim());
                cmd.Parameters.AddWithValue("@e", ddl_Era.DataValueField.Trim());
                cmd.Parameters.AddWithValue("@h", ddl_HeadofState.DataValueField.Trim());
                cmd.Parameters.AddWithValue("@d", ddl_Designer.DataValueField.Trim());
                cmd.Parameters.AddWithValue("@th", txt_Theme.Text.ToString());
                cmd.Parameters.AddWithValue("@m", ddl_Material.DataValueField.Trim());
                cmd.Parameters.AddWithValue("@w", txt_Weight.Text.Trim());
                cmd.Parameters.AddWithValue("@di", txt_Diameter.Text.Trim());
                cmd.Parameters.AddWithValue("@thi", txt_Thickness.Text.Trim());
                cmd.Parameters.AddWithValue("@p", txt_Punch.Text.ToString());
                cmd.Parameters.AddWithValue("@s", txt_Shape.Text.ToString());
                cmd.Parameters.AddWithValue("@o", txt_Obverse.Text.ToString());
                cmd.Parameters.AddWithValue("@r", txt_Reverse.Text.ToString());
                cmd.Parameters.AddWithValue("@ed", txt_Edge.Text.ToString());
                cmd.Parameters.AddWithValue("@pr", txt_PrivyMark.Text.ToString());
                cmd.Parameters.AddWithValue("@mi", txt_MintMark.Text.ToString());
                cmd.Parameters.AddWithValue("@de", txt_Details.Text.ToString());
                cmd.Parameters.AddWithValue("@Q", txt_Qty.Text.Trim());

                con.Open();
                cmd.ExecuteNonQuery(); 
                lbl_msg.Text = (String)GetGlobalResourceObject("Err_msg", "MsgDB3");
            }
            catch (Exception ex)
            {
                lbl_msg.Text = (String)GetGlobalResourceObject( "Err_msg", "MsgDB2")+" " +ex.Message;

            }
        }

    }

My table:

CREATE TABLE [dbo].[Coins] (
    [Coin_Id]     INT           IDENTITY (1, 1) NOT NULL,
    [Title]       NVARCHAR (50) NOT NULL,
    [Title_ar]    NVARCHAR (50) NOT NULL,
    [country]     INT           NULL,
    [value]       INT           NOT NULL,
    [Face_value]  INT           NOT NULL,
    [Year]        INT           NULL,
    [Era]         INT           NOT NULL,
    [HeadofState] INT           NOT NULL,
    [Designer]    INT           NOT NULL,
    [Theme]       NVARCHAR (50) NULL,
    [Material]    INT           NOT NULL,
    [Weight]      DECIMAL (18)  NULL,
    [Diameter]    DECIMAL (18)  NULL,
    [Thickness]   DECIMAL (18)  NULL,
    [Punch]       NVARCHAR (50) NULL,
    [Shape]       NVARCHAR (50) NULL,
    [Obverse]     NVARCHAR (50) NULL,
    [Reverse]     NVARCHAR (50) NULL,
    [Edge]        NVARCHAR (50) NULL,
    [Privy_mark]  NVARCHAR (50) NULL,
    [Mint_mark]   NVARCHAR (50) NULL,
    [Details]     NVARCHAR (50) NULL,
    [Qty]         INT           NULL,
    PRIMARY KEY CLUSTERED ([Coin_Id] ASC),
    CONSTRAINT [FK_coins_Material] FOREIGN KEY ([Material]) REFERENCES [dbo].[Material] ([Mat_Id]),
    CONSTRAINT [FK_coins_Designer] FOREIGN KEY ([Designer]) REFERENCES [dbo].[Designer] ([Des_Id]),
    CONSTRAINT [FK_coins_HeadofState] FOREIGN KEY ([HeadofState]) REFERENCES [dbo].[HeadofState] ([HoS_Id]),
    CONSTRAINT [FK_coins_Era] FOREIGN KEY ([Era]) REFERENCES [dbo].[Era] ([Era_Id]),
    CONSTRAINT [FK_coins_Face_value] FOREIGN KEY ([Face_value]) REFERENCES [dbo].[Face_value] ([FaV_Id]),
    CONSTRAINT [FK_coins_Country] FOREIGN KEY ([country]) REFERENCES [dbo].[Countries] ([Id])
);

The error is:

错误是:

Conversion failed when converting the nvarchar value 'id' to data type int.

将nvarchar值'id'转换为数据类型int时转换失败。

Could anyone help me in that? Thanks.

有人可以帮助我吗?谢谢。

2 个解决方案

#1


-1  

I think , your last parameter Qty is Int in Db but in the code you are getting this value from some text box "txt_Qty.Text.Trim()".. There the issue lies, try to use Convert.Toint32(txt_Qty.Text)

我想,你的最后一个参数Qty是Db中的Int,但在代码中你从某个文本框“txt_Qty.Text.Trim()”获得这个值。问题在于,尝试使用Convert.Toint32(txt_Qty.Text )

#2


0  

Grab QueryFirst. It will wrap your query in C#, with query parameters automatically mapped to the corresponding C# types. You can just let the compiler tell you if you're providing valid values.

抓住QueryFirst。它将您的查询包装在C#中,查询参数自动映射到相应的C#类型。您可以让编译器告诉您是否提供有效值。

#1


-1  

I think , your last parameter Qty is Int in Db but in the code you are getting this value from some text box "txt_Qty.Text.Trim()".. There the issue lies, try to use Convert.Toint32(txt_Qty.Text)

我想,你的最后一个参数Qty是Db中的Int,但在代码中你从某个文本框“txt_Qty.Text.Trim()”获得这个值。问题在于,尝试使用Convert.Toint32(txt_Qty.Text )

#2


0  

Grab QueryFirst. It will wrap your query in C#, with query parameters automatically mapped to the corresponding C# types. You can just let the compiler tell you if you're providing valid values.

抓住QueryFirst。它将您的查询包装在C#中,查询参数自动映射到相应的C#类型。您可以让编译器告诉您是否提供有效值。