The stored procedure for this could be:
CREATE PROCEDURE [ProcedureName]
@Parameter1 INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRANSACTION
IF EXISTS ( SELECT *
FROM [TableName]
WHERE <conditions> )
BEGIN
UPDATE [TableName]
SET [FieldName] = [Value]
WHERE <conditions>
END
ELSE
BEGIN
INSERT INTO <tablename> VALUES <............>
END
IF @@ROWCOUNT = 0
BEGIN
ROLLBACK TRANSACTION
END
ELSE
BEGIN
COMMIT TRANSACTION
END
END
And in Click Event of a button do this:
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection conn = <your connectionstring="">;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = conn;
String sqlQuery = "";
try
{
conn.Open();
for (int i = 0; i < yourdgv.RowCount - 1; i++)
{
sqlCommand.CommandText = "<yourprocedurename>";
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand .Parameters.AddWithValue("@parameter1", <value>);
sqlCommand.ExecuteNonQuery();
}
}
catch (Exception)
{
}
finally
{
conn.Close();
}
}
...
Plz. Let me know if this helps.