insert, update ,delete multiple rows using c# and sql at once with the help of list and function
ANS.
1. Create function:-
CREATE FUNCTION [dbo].[udf_CsvToInt]
(
@Array VARCHAR(max)
)
RETURNS @IntTable TABLE (IntValue INT)
AS
BEGIN
IF @Array <> '' BEGIN
DECLARE @separator char(1)
SET @separator = ','
DECLARE @separator_position INT
DECLARE @array_value VARCHAR(2000)
SET @array = @array + ','
WHILE patindex('%,%' , @array) <> 0 BEGIN
SELECT @separator_position = patindex('%,%' , @array)
SELECT @array_value = LEFT(@array, @separator_position - 1)
INSERT @IntTable Values (CAST(@array_value AS INT))
SELECT @array = stuff(@array, 1, @separator_position, '')
END
END
RETURN
end
2.update example
ALTER PROC spUpdateTable
@strID varchar(100)
AS
UPDATE tablename
SET IsActive = 1
WHERE tableID in (select * from dbo.udf_CsvToInt(@strID) )
3. C# button Click event
protected void btnTagIndexPage_Click(object sender, EventArgs e)
{
List<String> ltTag = new List<string>();
foreach (GridViewRow r in gvFitnessGyan.Rows)
{
CheckBox chkAttEmp = (CheckBox)r.FindControl("chkAttEmp");
Label lblID = (Label)r.FindControl("lblID");
bo.strID = Convert.ToString(ViewState["ID"]);
if (chkAttEmp.Checked == true)
{
ltTag.Add(lblID.Text);
}
}
String Category = String.Join(",", ltTag.ToArray());
}
ANS.
1. Create function:-
CREATE FUNCTION [dbo].[udf_CsvToInt]
(
@Array VARCHAR(max)
)
RETURNS @IntTable TABLE (IntValue INT)
AS
BEGIN
IF @Array <> '' BEGIN
DECLARE @separator char(1)
SET @separator = ','
DECLARE @separator_position INT
DECLARE @array_value VARCHAR(2000)
SET @array = @array + ','
WHILE patindex('%,%' , @array) <> 0 BEGIN
SELECT @separator_position = patindex('%,%' , @array)
SELECT @array_value = LEFT(@array, @separator_position - 1)
INSERT @IntTable Values (CAST(@array_value AS INT))
SELECT @array = stuff(@array, 1, @separator_position, '')
END
END
RETURN
end
2.update example
ALTER PROC spUpdateTable
@strID varchar(100)
AS
UPDATE tablename
SET IsActive = 1
WHERE tableID in (select * from dbo.udf_CsvToInt(@strID) )
3. C# button Click event
protected void btnTagIndexPage_Click(object sender, EventArgs e)
{
List<String> ltTag = new List<string>();
foreach (GridViewRow r in gvFitnessGyan.Rows)
{
CheckBox chkAttEmp = (CheckBox)r.FindControl("chkAttEmp");
Label lblID = (Label)r.FindControl("lblID");
bo.strID = Convert.ToString(ViewState["ID"]);
if (chkAttEmp.Checked == true)
{
ltTag.Add(lblID.Text);
}
}
String Category = String.Join(",", ltTag.ToArray());
}
Comments
Post a Comment