Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Having trouble connecting with my datasource

Having trouble connecting with my datasource

Scheduled Pinned Locked Moved C#
helpcsharpdatabasevisual-studiodata-structures
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deadEddie
    wrote on last edited by
    #1

    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. :confused:

    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;
        }
    
    H A 2 Replies Last reply
    0
    • D deadEddie

      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. :confused:

      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;
          }
      
      H Offline
      H Offline
      Hum Dum
      wrote on last edited by
      #2

      deadEddie wrote:

      string selectSql = "SELECT Count(*) FROM AccountTransactions WHERE UserID = @UserID";

      You are selecting column which are not present is select statement.

      deadEddie wrote:

      returnArray[0] = Convert.ToString(dataSet.Tables["AccountTransactions"].Rows[lastRow]["Balance"]); returnArray[1] = Convert.ToString(dataSet.Tables["AccountTransactions"].Rows[lastRow]["TransactionNo"]);

      For accessing balance and transaction include them is select statement, like

      Select Balance, TransactionNo FROM AccountTransactions WHERE UserID = @UserID;

      D 1 Reply Last reply
      0
      • D deadEddie

        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. :confused:

        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;
            }
        
        A Offline
        A Offline
        AnnieCalvert
        wrote on last edited by
        #3

        It may help you .read it. http://www.dapfor.com/en/net-suite/net-grid/tutorial

        D 1 Reply Last reply
        0
        • H Hum Dum

          deadEddie wrote:

          string selectSql = "SELECT Count(*) FROM AccountTransactions WHERE UserID = @UserID";

          You are selecting column which are not present is select statement.

          deadEddie wrote:

          returnArray[0] = Convert.ToString(dataSet.Tables["AccountTransactions"].Rows[lastRow]["Balance"]); returnArray[1] = Convert.ToString(dataSet.Tables["AccountTransactions"].Rows[lastRow]["TransactionNo"]);

          For accessing balance and transaction include them is select statement, like

          Select Balance, TransactionNo FROM AccountTransactions WHERE UserID = @UserID;

          D Offline
          D Offline
          deadEddie
          wrote on last edited by
          #4

          :) Thanks Hum Dum. Of course that fixed my problem. DE

          1 Reply Last reply
          0
          • A AnnieCalvert

            It may help you .read it. http://www.dapfor.com/en/net-suite/net-grid/tutorial

            D Offline
            D Offline
            deadEddie
            wrote on last edited by
            #5

            Thanks Annie. Good read

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups