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

时间:2016-08-11 11:02:12

标签: c# sql asp.net .net

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

我的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;

            }
        }

    }

我的表:

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

错误是:

  

转换nvarchar值时转换失败' id'到数据类型   中间体

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

我认为,您的最后一个参数Qty是Db中的Int,但是在代码中您从某个文本框“txt_Qty.Text.Trim()”获取此值。问题在于,尝试使用{{1} }