المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Having trouble connecting with my datasource



C# Programming
07-02-2012, 05:23 PM
Hey there, I'm sure there is some simple newbie problem with my code, but I have not been able to figure it out and was hoping another set of eyes would help.

I'm working on a financial accounting tool that so far has two tables, a Users table and an AccountsTransactions table, The below method is from my DatabaseCommunicator class. It builds fine but crashes with a message that the "Balance" column is not in the AccountsTransactions Table. Since I know that the column is in fact in the table, the only thing I can think of as being the problem is that I'm not actually connecting to the datasource and filling the dataset. Can anyone see where I'm going wrong?

Thanks in advance. http://www.barakasoft.com/script/Forums/Images/smiley_confused.gif

public string[] CreateNewTransaction(int UserID) // note the use of string[] as the method return type { // create an array to hold method results string[] returnArray = new string[2]; string selectSql = "SELECT Count(*) FROM AccountTransactions WHERE UserID = @UserID"; // new SqlConnection connect = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Users\\Ed\\Documents\\Visual Studio 2010\\Projects\\School Projects\\Accounts\\Accounts\\Accounts.mdf';Integrated Security=True;User Instance=True"); // initialize SqlDataAdapter dataAdapter = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand(selectSql, connect); dataAdapter.SelectCommand = cmd; // parameters for cmd cmd.Parameters.AddWithValue("@UserID", UserID); // create a new DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet, "AccountTransactions"); // create variable equal to the count of rows in the database and set the DataRow to that row number int lastRow = dataSet.Tables["AccountTransactions"].Rows.Count-1; DataRow dRow = dataSet.Tables["AccountTransactions"].Rows[lastRow]; returnArray[0] = Convert.ToString(dataSet.Tables["AccountTransactions"].Rows[lastRow]["Balance"]); returnArray[1] = Convert.ToString(dataSet.Tables["AccountTransactions"].Rows[lastRow]["TransactionNo"]); connect.Dispose(); return returnArray; }