Reputation: 237
I'm trying to Insert and update multiple rows from a Datatable into database table. Below is the code to pass Datatable to Stored procedure:
DataTable dtUserRights = new DataTable();
PropertyInfo[] piT = typeof(clsRightsAL).GetProperties(); //Gets all column values with there datatype
foreach (PropertyInfo pi in piT)
{
//create a datacolumn for each property
DataColumn dc = new DataColumn(pi.Name, pi.PropertyType);
dtUserRights.Columns.Add(dc);
}
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
for (int item = 0; item < objUserRightsALLst.Count; item++)
{
DataRow dr = dtUserRights.NewRow();
for (int property = 0; property < dtUserRights.Columns.Count; property++)
{
dr[property] = piT[property].GetValue(objUserRightsALLst[item], null);
}
dtUserRights.Rows.Add(dr);
}
objCmd = new SqlCommand();
objCmd.CommandText = "sp_AD_InsertUpdateGroupRights";
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Connection = con;
objCmd.Parameters.Clear();
SqlParameter tableParameter = objCmd.Parameters.AddWithValue("@GroupRightslist", dtUserRights);
tableParameter.SqlDbType = SqlDbType.Structured;
intCount = objCmd.ExecuteNonQuery();
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
catch (Exception e)
{
throw e;
}
Below is the SP I'm calling:
CREATE PROCEDURE [dbo].[sp_AD_InsertUpdateGroupRights]
@GroupRightslist as [dbo].[GroupRightsList] ReadOnly
AS
BEGIN
if Not Exists(Select intID from tblADGroupRights where intID = (Select l.intID from @GroupRightslist l))
Begin
insert into tblADGroupRights(IntGroupID,intMenuID,blAdd,blEdit,blView,blGrant,blStatus,strUserCreated
) Select l.IntGroupID,l.intMenuID,l.blAdd,l.blEdit,l.blView,l.blGrant,l.blStatus,
l.strBy from GroupRightslist l
End
else
begin
update A set blAdd = l.blAdd,blEdit = l.blEdit,blView = l.blView,blGrant = l.blGrant
FROM tblADGroupRights A INNER JOIN @GroupRightslist l on A.intID = l.intID
where A.intID = l.intID
end
END
When im passing single row to Stored procedure the data is getting inserted, but when datatable is having multiple records , i'm getting Exception:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
And my update Query is not at all working.
My User Defined Table Type:
CREATE TYPE [dbo].[GroupRightsList] AS TABLE(
[intID] [int] NULL,
[intProfileID] [int] NULL,
[IntGroupID] [int] NULL,
[intMenuID] [int] NULL,
[blAdd] [bit] NULL,
[blEdit] [bit] NULL,
[blView] [bit] NULL,
[blGrant] [bit] NULL,
[blStatus] [bit] NULL,
[strBy] [nvarchar](max) NULL
)
I'm aware that this exception usually raises when the Select query return more than 1 value and we have not handled it properly, but in my scenario as Im using User Type table i have seen many sites where they have shown it in the same way i have done.
Upvotes: 1
Views: 482
Reputation: 32701
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
it is purely an sp error. It means that in your sp you have a subquery that is returning more than one record which is not permitted. Most probably below is the line
Select intID from tblADGroupRights where intID = (Select l.intID from @GroupRightslist l)
Either change it to
Select intID from tblADGroupRights where intID = (Select Top 1 l.intID from @GroupRightslist l)
or
Select intID from tblADGroupRights where intID in (Select l.intID from @GroupRightslist l)
Upvotes: 1