1

So I am trying to do an application that makes a simple transaction from account 1 to account 2

here is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Transactions;

namespace BankTransactions
{
    public class Transactions
    {
        public bool Transfer(int fromAcno, int toAcno, decimal amt)
        {
            try
            {
                string cs = ConfigurationManager.ConnectionStrings["Integrated Security = SSPI; Persist Security Info = False; Initial Catalog = NORTHWND; Data Source = CRISTI"].ConnectionString;
                SqlConnection cn = new SqlConnection(cs);
                cn.Open();
                SqlTransaction trans;
                trans = cn.BeginTransaction();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.Transaction = trans;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = String.Format("update PSBank set amount=amount+{0} where accountno={1}", amt, toAcno);
                cmd.ExecuteNonQuery();
                cmd.CommandText = String.Format("update PSBank set amount=amount-{0} where accountno={1}", amt, fromAcno);
                cmd.ExecuteNonQuery();
                decimal balance;
                balance = (decimal)cmd.ExecuteScalar();
                decimal originalBalance;
                originalBalance = balance - amt;
                bool flag = true;
                if (originalBalance >= 5000)
                {
                    trans.Commit();
                    flag = true;
                }

                else
                {
                    trans.Rollback();
                    flag = false;
                }

                cn.Close();
                return flag;
            }

            catch (Exception ex)
            {
                return false;
            }
        }


    }
}

and here is the winform code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BankTransactions;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int fromAcno = int.Parse(txtFromAcno.Text);
            int toAcno = int.Parse(txtToAcno.Text);
            decimal amount = decimal.Parse(txtAmount.Text);

            Transactions obj = new Transactions();
            if (obj.Transfer(fromAcno, toAcno, amount) ==true)
            {
                MessageBox.Show("Amount transferred succesfully!", "Confirm");
            }
            else
            {
                MessageBox.Show("Error!", "Error");
            }
        }
    }
}

The form has 3 text boxes: txtFromAcno, txtToAcno and txtAmount. I have the database called NORTHWND with the table PSBank where I have these two accounts. I am getting the error message, not "amount transferred successfully". I am not sure how to make the connection to the DB in the connection string.

  string cs = ConfigurationManager.ConnectionStrings["Integrated Security = SSPI; Persist Security Info = False; Initial Catalog = NORTHWND; Data Source = CRISTI"].ConnectionString;

How do I format it properly?

4
  • Place a breakpoint inside the catch part, give us information about what exception is being thrown. Right now you just throw away all the juicy tidbits that can help you debug by returning false. If you really want to debug, remove the try/catch entirely and try running the code, problems will become much more apparent. Commented Dec 10, 2015 at 18:53
  • I can't get it to display the error. I suspect the connection is not being made to the DB. Is the connection string any good? Commented Dec 10, 2015 at 19:00
  • Not the way you have it, if you want to specify it there, just keep the cs = "Integrated Security.." part, you don't need the configuration manager stuff. Commented Dec 10, 2015 at 19:01
  • Possible duplicate of C# DLL config file Commented Dec 10, 2015 at 22:54

1 Answer 1

1

Put the connection string in app.config like so:

<connectionStrings>
    <add name="MyConnection" 
        connectionString="Integrated Security = SSPI; Persist Security Info = False; Initial Catalog = NORTHWND; Data Source = CRISTI"
        providerName="System.Data.SqlClient" >
</connectionStrings>

And then reference it by name in code:

string cs = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
Sign up to request clarification or add additional context in comments.

1 Comment

It still does not work. How should I make the connection string again??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.