Click here to Skip to main content
11,706,455 members (47,973 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# SQL-Server ADO ADO.NET
i can't get 2 query
just working string query
and query dont

      internal bool saveNewPayment(Guid ID, Guid creditID, decimal paymentAmount, DateTime dateTime)
          bool flag = true;
          using (SqlConnection con = new SqlConnection(constring))
              SqlTransaction sqlTranc = con.BeginTransaction();
              SqlCommand com = con.CreateCommand();
              com.Transaction = sqlTranc;
                  string payAmount = paymentAmount.ToString(CultureInfo.InvariantCulture.NumberFormat);

                 string query = string.Format("INSERT INTO Payment (ID, CreditsID, Amount, PaymenDate)" + " VALUES ('{0}', '{1}', '{2}', '{3}')",
                   ID, creditID, payAmount, dateTime.ToString("MM/dd/yyyy"));
                  com.CommandText = query;
                 query = string.Format("UPDATE Credits SET Balance = (Balance - {0}) WHERE ID = '{1}'", payAmount, creditID);
                  com.CommandText = query;

              catch (Exception)
                  flag = false;
                  if (con.State == System.Data.ConnectionState.Open)
          return flag;
Posted 28-Nov-12 12:23pm
digimanus at 28-Nov-12 18:28pm
you do 2 commands over 1 statement. Second query will not work if it is first one. Than no entry in table.
Adam R Harris at 11-Dec-12 15:18pm
What is the value of creditId?
What is the field type of ID on the Credits table?
What command isn't running?
- Does your insert run?
- Does your update run?
Rohit Shrivastava at 11-Dec-12 16:53pm
what exception is coming and on which line, did not understand the question.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

since you use the con.BeginTransaction() before the beginning of statement the real insert will not complete until you make call to sqlTranc.Commit();

so its obvious that the creditID which you are trying to update is not available in the table and it failed to update.
move the Commit statement just after the insert is completed and try running the update query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 202
1 Sergey Alexandrovich Kryukov 135
2 chainerlt 110
3 Mika Wendelius 80
4 ppolymorphe 71
0 OriginalGriff 9,190
1 Sergey Alexandrovich Kryukov 8,407
2 CPallini 5,189
3 Maciej Los 4,756
4 Mika Wendelius 3,686

Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100