0

我有 3 个文本框和添加按钮。单击按钮时,它应该将文本框中的信息插入到数据库中。问题是单击它时出现以下错误:Conversion failed when converting the varchar value '@id' to data type int. 这是添加按钮 onclick 事件:

SqlConnection conn = new SqlConnection("Data Source="Name";Initial Catalog="Name";Integrated Security=True");
            SqlCommand cmd = new SqlCommand("addData", conn);
            conn.Open();
            cmd.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = Convert.ToInt32(txtID.Text);
            cmd.Parameters.Add("@FName", System.Data.SqlDbType.VarChar).Value = txtFName.Text;
            cmd.Parameters.Add("@LName", System.Data.SqlDbType.VarChar).Value = txtLName.Text;
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader rd = cmd.ExecuteReader();
            GridView1.DataSource = rd;
            GridView1.DataBind();

我也有一个添加数据的存储过程,我想我可能在这里犯了一个错误:

USE ['Name']
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[addData]
@id int,
@FName varchar(30),
@LName varchar(30)
as
begin
Insert into Customer(ID, FName, LName)
Values('@id', '@FName', '@LName')
end

请帮我找出我的错误。

4

2 回答 2

4

你需要更换

Values('@id', '@FName', '@LName')

Values(@id, @FName, @LName)
于 2013-09-21T20:48:51.507 回答
1

您缺少@登录参数。

SqlConnection conn = new SqlConnection("Data Source="Name";Initial Catalog="Name";Integrated Security=True");
            SqlCommand cmd = new SqlCommand("addData", conn);
            conn.Open();
            cmd.Parameters.Add("@id", System.Data.SqlDbType.Int)
                          .Value = Convert.ToInt32(txtID.Text);
            cmd.Parameters.Add("@FName",System.Data.SqlDbType.VarChar)
                          .Value = txtFName.Text;
            cmd.Parameters.Add("@LName", System.Data.SqlDbType.VarChar)
                          .Value = txtLName.Text;
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader rd = cmd.ExecuteReader();
            GridView1.DataSource = rd;
            GridView1.DataBind();
            conn.Close()//don't forget to close conn
于 2013-09-21T20:35:18.943 回答