当我点击按钮时,此错误显示。帮我解决这个错误。我想将数据保存在两个表中

时间:2015-12-24 12:21:32

标签: c# sql asp.net

  

子查询返回的值超过1。这是不允许的   子查询跟随=,!=,<,< =,>,> =或当子查询用作   表达。

我的aspx代码是..

protected void txtsave_Click(object sender, EventArgs e)
{
        try
        {

            SqlCommand cmd = new SqlCommand("Insert_MainTable", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name", txtname.Text);
            cmd.Parameters.AddWithValue("@Addr", txtaddr.Text);
            cmd.Parameters.AddWithValue("@Qual", txtqual.Text);
            cmd.Parameters.AddWithValue("@Dob", txtdob.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch { }
    }
}

我的程序是......

USE [Harish]
GO
/****** Object:  StoredProcedure [dbo].[Insert_MainTable]    Script Date: 24-12-2015 16:36:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Insert_MainTable]


@Name varchar(50),
@Addr varchar(50),

@Qual varchar(50),
@Dob varchar(50)
as begin


insert into MainPracticeTable(Name,Addr) values(@Name,@Addr)

declare @Eid int
set @Eid=(select Id from MainPracticeTable where Name=@Name)


insert into  PracticeTable(Eid,Qual,Dob)values(@Eid,@Qual,@Dob)

end

提前感谢。

1 个答案:

答案 0 :(得分:3)

此语句返回多行

select Id from MainPracticeTable where Name=@Name

因此,要么修复where条款,要么添加top关键字order by

set @Eid=(select TOP 1 Id from MainPracticeTable where Name=@Name order by somecol)

select @Eid= Id from MainPracticeTable where Name=@Name