Link to home
Start Free TrialLog in
Avatar of Adnan
AdnanFlag for Norway

asked on

How to loop in strinbuilder to get each id i have appended in my strinbuilder......

Hi

I want to loop thrue all the ids i have appended in my stringbuilder and execute sql query to get all rows in my gridview.

But when i run the code i get only the last id or row and not all rows i want to display in my Grid...?
public DsAccount GetImpFileAccountNo(Operator myOperator, List<int> impFile_IdList)
        {
            DsAccount ds = new DsAccount(); // 12,13,85,65
            ds.EnforceConstraints = false;
 
            StringBuilder sb = new StringBuilder();
            sb.Append(impFile_IdList);
            foreach(int fileimp in impFile_IdList)
            {
                if (sb.Length > 0)
                {
                    sb.Append(",");
                    sb.Append(fileimp.ToString());
 
                }
                
                string sql = "SELECT  Account.AccountNo, sum(MatchItem.Amount) AS SumAmount, Account.Balance " +
                          "FROM ImpFile INNER JOIN " +
                              "MatchItem ON ImpFile.ImpFile_ID = MatchItem.ImpFile_ID INNER JOIN " +
                              "Account ON MatchItem.Account_ID = Account.Account_ID " +
                              "WHERE ImpFile.ImpFile_ID IN (" + fileimp + ")" +
                              "Group by AccountNo,Account.Balance ";
 
            myDataAccess.Fill(myDataAccess.GetNewDataAdapter(myDataAccess.GetNewCommand(sql)), ds, "Account");
            }
 
           
            return ds ;
        }

Open in new window

Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image


public DsAccount GetImpFileAccountNo(Operator myOperator, List<int> impFile_IdList)
        {
            DsAccount ds = new DsAccount(); // 12,13,85,65
            ds.EnforceConstraints = false;
 
            StringBuilder sb = new StringBuilder();
            //sb.Append(impFile_IdList); 
            foreach(int fileimp in impFile_IdList)
            {
                if (sb.Length > 0)
                {
                    sb.Append(",");
                    sb.Append(fileimp.ToString());
 
                }
             } //close the foreach here and SELECT just once
 
                string sql = "SELECT  Account.AccountNo, sum(MatchItem.Amount) AS SumAmount, Account.Balance " +
                          "FROM ImpFile INNER JOIN " +
                              "MatchItem ON ImpFile.ImpFile_ID = MatchItem.ImpFile_ID INNER JOIN " +
                              "Account ON MatchItem.Account_ID = Account.Account_ID " +
                              "WHERE ImpFile.ImpFile_ID IN (" + fileimp + ")" +
                              "Group by AccountNo,Account.Balance ";
 
            myDataAccess.Fill(myDataAccess.GetNewDataAdapter(myDataAccess.GetNewCommand(sql)), ds, "Account");
            
 
           
            return ds ;
        }

Open in new window

Avatar of Adnan

ASKER

thanks for replay :)

But i get an error on the "WHERE ImpFile.ImpFile_ID IN (" + fileimp + ")" +

it says that the fileimp does not exist in the current context......??
sorry I missed that.  It should be
"WHERE ImpFile.ImpFile_ID IN (" + sb.ToString()+ ")" +
Avatar of Adnan

ASKER

oki i didet like the code i pasted below but still i only get one row ini my grid.....
int test;
        public DsAccount GetImpFileAccountNo(Operator myOperator, List<int> impFile_IdList)
        {
            DsAccount ds = new DsAccount(); // 12,13,85,65
            ds.EnforceConstraints = false;
 
            StringBuilder sb = new StringBuilder();
            //sb.Append(impFile_IdList);
            
            foreach(int fileimp in impFile_IdList)
            {
                if (sb.Length > 0)
                {
                    sb.Append(",");
                    sb.Append(fileimp.ToString());    
                }
                test = fileimp;
            }
            string sql = "SELECT  Account.AccountNo, sum(MatchItem.Amount) AS SumAmount, Account.Balance " +
                        "FROM ImpFile INNER JOIN " +
                            "MatchItem ON ImpFile.ImpFile_ID = MatchItem.ImpFile_ID INNER JOIN " +
                            "Account ON MatchItem.Account_ID = Account.Account_ID " +
                            "WHERE ImpFile.ImpFile_ID IN (" + test + ")" +
                            "Group by AccountNo,Account.Balance ";
 
            myDataAccess.Fill(myDataAccess.GetNewDataAdapter(myDataAccess.GetNewCommand(sql)), ds, "Account");
           
            return ds ;
        }

Open in new window

Avatar of Adnan

ASKER

hmm oki, i will try:  sb.ToString()!!!
fileimp is only ever a single integer.  the appended version (e.g. 1,2,3,4) is in sb.
Avatar of Adnan

ASKER

know i run the code as you showed above, with sb.ToString(), but now i get another error message: Incorrect syntax near ')'.
 string sql = "SELECT  Account.AccountNo, sum(MatchItem.Amount) AS SumAmount, Account.Balance " +
                        "FROM ImpFile INNER JOIN " +
                            "MatchItem ON ImpFile.ImpFile_ID = MatchItem.ImpFile_ID INNER JOIN " +
                            "Account ON MatchItem.Account_ID = Account.Account_ID " +
                            "WHERE ImpFile.ImpFile_ID IN (" + sb.ToString() + ")" +
                            "Group by AccountNo,Account.Balance ";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adnan

ASKER

thanks alot
Avatar of Adnan

ASKER

but still i do not get all files i want to be displayed in my grid, i still only can se the last row from dataset... ?=
still having the problem?

If so, please post the actual contents of the string SQL.

thanks.
Avatar of Adnan

ASKER

hmm oki, here is the code of form where i have the grid i want to display my selected rows.....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Adra.Admin;
using Adra.Language;
using Adra.AMEngine;
 
namespace Adra.AMDesk
{
    public partial class UndoImpAccountInfo : Form
    {
        private Operator myOperator;
        private string myLanguageName = "";
        private System.Reflection.Assembly assembly = typeof(UndoImpAccountInfo).Assembly;
        
        string fname;
        string getinfo;
        decimal amount;
        decimal blnc;
        string acc;
 
        public UndoImpAccountInfo(Operator oper, string name, string account, decimal samount,decimal balance)
        {
            InitializeComponent();
            myOperator = oper;
            myLanguageName = oper.LanguageName;
            UpdateLanguage();
            fname = name;
            acc = account;
            blnc = balance;
            amount = samount;
            GetFileInfo(fname, acc,amount);
            
        }
        string delimeter;
        public void GetFileInfo(string fname, string acc, decimal amount)
        {
            UndoImport undoImp = new UndoImport(myOperator);
            getinfo = undoImp.getInfo();
            lblFname.Text = fname;
 
            foreach (DataGridViewRow row in dgvUndoImportInfo.Rows)
            {
                row.Cells["AccountNo"].Value = acc;
                row.Cells["Sum"].Value = amount;
                row.Cells["Balance"].Value = blnc;
 
                decimal blncAfterUndo = decimal.Subtract(blnc, amount);
                row.Cells["BalanceAfterUndo"].Value = blncAfterUndo;
            } 
        }
 
        public string GetValue(string id)
        {
            return LanguageResource.GetText(id, myLanguageName, "Adra.AMDesk.Strings", assembly);
        }
 
        private void UpdateLanguage()
        {
            //dgvUndoImportInfo Column name
            dgvUndoImportInfo.Columns["AccountNo"].HeaderText = GetValue("account");
            dgvUndoImportInfo.Columns["Sum"].HeaderText = GetValue("sum");
            dgvUndoImportInfo.Columns["Balance"].HeaderText = GetValue("balance");
            dgvUndoImportInfo.Columns["BalanceAfterUndo"].HeaderText = GetValue("balancebfterundo");
 
            //btn and labels
            btnClose.Text = GetValue("cancel");
            lblaccinfo.Text = GetValue("importaccountinfo");
            lblFilename.Text = GetValue("filename");  
        }
 
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Open in new window

Avatar of Adnan

ASKER

And here is the getInfo function wich is in another form called UndoImport.cs
        public string getInfo()
        {
        
            List<int> impFile_IdList = new List<int>();
            foreach (DataGridViewRow row in this.gridImports.SelectedRows)
            {
                fileName = row.Cells["FileName"].Value.ToString() + "\r\n";
                int impID = (int)row.Cells["ImpFile_ID"].Value;
                impFile_IdList.Add(impID);
                //ImpAccountNo = AmEngine.Instance.GetImpFileAccountNo(myOperator, impFile_IdList);
 
            }
 
            ImpAccountNo = AmEngine.Instance.GetImpFileAccountNo(myOperator, impFile_IdList);
            foreach (DsAccount.AccountRow dr in ImpAccountNo.Account.Rows)
            {
                account = dr.AccountNo;
                balance = (decimal)dr.Balance;
                sAmount = (decimal)dr["SumAmount"];
                //liste.Add("\r\n" + dr.AccountNo + "  " + dr["SumAmount"].ToString());
            }
            return impFile_IdList.ToString();
        }

Open in new window

after the line
string sql = " ...

What are the actual contents of that string?  And how many items were in your list<int> when you trace into the function?
Avatar of Adnan

ASKER

its like this: i have a application were i can import different type of files, and in my main form i show all the imported files in my grid, and know iam making a function where i can go and selct one or more imported files in the grid and click on show info button to se another form where i have a grid i want to display info about those selected import files from main form grid...?

And actual contents of  string is import file ids......impId, it depends on how many import you have selected to se informasion about...?!!!

I hope that helps you... :)
Avatar of Adnan

ASKER

here is the button click event...
 private void button1_Click(object sender, EventArgs e)
        {
            getInfo();
            string navn = fileName;
            UndoImpAccountInfo unImpAccInfo = new UndoImpAccountInfo(myOperator, navn, account, sAmount, balance);
            unImpAccInfo.ShowDialog();
        }

Open in new window

Avatar of Adnan

ASKER

i think the problom is in the function below , wich only add one row.....
 public void GetFileInfo(string fname, string acc, decimal amount)
        {
            UndoImport undoImp = new UndoImport(myOperator);
            getinfo = undoImp.getInfo();
            lblFname.Text = fname;
           
                //foreach (DataGridViewRow row in dgvUndoImportInfo.Rows)
            foreach (DataGridViewRow row in getinfo.ToString())
                {
                    //row.Cells["AccountNo"].Value = acc;
                    //row.Cells["Sum"].Value = amount;
                    //row.Cells["Balance"].Value = blnc;
 
                    //decimal blncAfterUndo = decimal.Subtract(blnc, amount);
                    //row.Cells["BalanceAfterUndo"].Value = blncAfterUndo;
                }
        }

Open in new window

Line 8 in your last post cannot be correct:

foreach (DataGridViewRow row in getinfo.ToString())

There are not DataGridViewRow's in a String.