[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-04-17
18
Medium Priority
?
180 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Adnan
  • 11
  • 7
18 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24166385

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

0
 

Author Comment

by:Adnan
ID: 24166411
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......??
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24166419
sorry I missed that.  It should be
"WHERE ImpFile.ImpFile_ID IN (" + sb.ToString()+ ")" +
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Adnan
ID: 24166429
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

0
 

Author Comment

by:Adnan
ID: 24166436
hmm oki, i will try:  sb.ToString()!!!
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24166468
fileimp is only ever a single integer.  the appended version (e.g. 1,2,3,4) is in sb.
0
 

Author Comment

by:Adnan
ID: 24166495
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

0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 2000 total points
ID: 24166536
another error.  sb was never actually appending anything.  change it per below.

           if (sb.Length > 0)
                {
                    sb.Append(",");  
                }
 
            sb.Append(fileimp.ToString());  

Open in new window

0
 

Author Closing Comment

by:Adnan
ID: 31571394
thanks alot
0
 

Author Comment

by:Adnan
ID: 24166604
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... ?=
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24166713
still having the problem?

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

thanks.
0
 

Author Comment

by:Adnan
ID: 24166765
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

0
 

Author Comment

by:Adnan
ID: 24166779
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

0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24166836
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?
0
 

Author Comment

by:Adnan
ID: 24166899
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... :)
0
 

Author Comment

by:Adnan
ID: 24166935
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

0
 

Author Comment

by:Adnan
ID: 24167399
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

0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24168517
Line 8 in your last post cannot be correct:

foreach (DataGridViewRow row in getinfo.ToString())

There are not DataGridViewRow's in a String.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question