Hello Sir,
I have some confusion/problem with SQL Transaction in C#.
I have 2 tables in MS SQL Database.
1. Customer (Id, ChildId, ParentId, Position) (Like Binary Tree Structure)
2. Bonus (Id, date, Income, TDS, Income_type, LeftPoint, RightPoint)
How can I use different methods (which includes Insert query) with SQLtransaction
private void CalculateIncome()
{
Self_Bonus("L7890", DateTime.Now);
Direct_Bonus("L7890", DateTime.Now);
Matching_Points("R5678", DateTime.Now, "Left", 100);
}
private void Self_Bonus(string Self_ID, DateTime Pdate)
{
decimal Amount = 100;
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "Insert into Bonus(Id, date, Income, TDS, Income_type) values(@Id, @Idate, @Income, @TDS, @Income_type)";
com.Parameters.AddWithValue("@Id", Self_ID);
com.Parameters.AddWithValue("@Idate", Pdate);
com.Parameters.AddWithValue("@Income", Amount);
com.Parameters.AddWithValue("@TDS", (Amount * 5)/100);
com.Parameters.AddWithValue("@Income_type", "Self Bonus");
con.Open();
com.ExecuteNonQuery();
con.Close();
}
private void Direct_Bonus(string Self_ID, DateTime Pdate)
{
decimal Amount = 100;
SqlDataAdapter da = new SqlDataAdapter("SELECT ParentId FROM Customer WHERE (ChildId = '" + Self_ID + "')", con);
DataSet ds = new DataSet();
da.Fill(ds);
string ParentID = ds.Tables[0].Rows[0]["ParentId"].ToString();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "Insert into Bonus(Id, date, Income, TDS, Income_type) values(@Id, @Idate, @Income, @TDS, @Income_type)";
com.Parameters.AddWithValue("@Id", ParentID);
com.Parameters.AddWithValue("@Idate", Pdate);
com.Parameters.AddWithValue("@Income", Amount);
com.Parameters.AddWithValue("@TDS", (Amount * 5)/100);
com.Parameters.AddWithValue("@Income_type", "Direct Bonus");
con.Open();
com.ExecuteNonQuery();
con.Close();
}
private void Matching_Points(string Parent_Id, DateTime Pdate, string Position, decimal Points)
{
Matching_P:;
decimal Left_Points = 0;
decimal Right_Points = 0;
if (Position == "Left")
Left_Points = Points;
if (Position == "Right")
Right_Points = Points;
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "Insert into Bonus(Id, Idate, Income_type, LeftPoint, RightPoint) values(@Id, @Idate, @Income_type, @LeftPoint, @RightPoint)";
com.Parameters.AddWithValue("@Id", Parent_Id);
com.Parameters.AddWithValue("@Idate", Pdate);
com.Parameters.AddWithValue("@Income_type", "Matching Points");
com.Parameters.AddWithValue("@LeftPoint", Left_Points);
com.Parameters.AddWithValue("@RightPoint", Right_Points);
if (con.State == ConnectionState.Open)
con.Close();
con.Open();
com.ExecuteNonQuery();
con.Close();
com.Parameters.Clear();
xy:;
SqlDataAdapter da = new SqlDataAdapter("SELECT ParentId, Position FROM Customer WHERE (ChildId = '" + Self_ID + "')", con);
DataSet ds = new DataSet();
da.Fill(ds);
string ParentID = ds.Tables[0].Rows[0]["ParentId"].ToString();
string Position = ds.Tables[0].Rows[0]["ParentId"].ToString();
if (ParentID == "Root")
return;
else
goto Matching_P;
}