How can I refer to controls which are created runtime in c#? - c#

I have 2 tables in my database one of which holds things and the other prices. I designed a form which retrieves thing names as checkboxes and adds textboxes against them. They are all created in runtime when the form loads. They all are named in loops accordingly and when form loads I can see them arranged in the way I wanted to. But I cannot refer them in design time in the form as you can see in below where TextBox1.Text is. May you suggest me how I can do this?. Thanks by now. Below is the code I wrote for controls:
public Form4()
{
InitializeComponent();
SqlConnection conn = new SqlConnection("Data Source=ZEFIRAN\\SQLEXPRESS; Initial Catalog=Lab; Integrated Security=true");
conn.Open();
string say = "SELECT COUNT(DISTINCT Thing) FROM Sale";
SqlCommand cmd = new SqlCommand(say, conn);
Int32 ts = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
for (int i = 0; i < ts; i++)
{
TextBox tb = new TextBox();
tb.Width = 50;
tb.Height = 25;
tb.Name = "TextBox" + (i + 1).ToString();
tb.Text = i + 1 + ". Test";
panel1.Controls.Add(tb);
int x = 150;
int y = 40;
tb.Location = new System.Drawing.Point(x, 40 + i * y);
}
for (int i = 0; i < ts; i++)
{
TextBox tb = new TextBox();
tb.Width = 50;
tb.Height = 25;
tb.Name = "TextBox" + (2 * i + 1).ToString();
tb.Text = i + 1 + ". Test";
panel1.Controls.Add(tb);
int x = 210;
int y = 40;
tb.Location = new System.Drawing.Point(x, 40 + i * y);
}
List<string> seyler = new List<string>();
conn.Open();
string query = "SELECT Thing FROM Things ORDER BY TNo";
using (SqlCommand cmdtest = new SqlCommand(query, conn))
{
using (SqlDataReader dr = cmdtest.ExecuteReader())
{
while (dr.Read())
{
seyler.Add(dr.GetString(0));
}
}
}
string toplam = "SELECT COUNT(Thing) FROM Things";
SqlCommand tplm = new SqlCommand(toplam, conn);
Int32 top1 = Convert.ToInt32(tplm.ExecuteScalar());
for (int j = 0; j < top1; j++)
{
CheckBox chb = new CheckBox();
panel1.Controls.Add(chb);
int t = 50;
int u = 40;
chb.Location = new System.Drawing.Point(t, 40 + j * u);
chb.Width = 100;
chb.Height = 25;
chb.Text = seyler[j];
chb.Name = "cb" + (j + 1).ToString();
}
conn.Close();
TextBox1.Text = "";
}

Related

Select data from SQL based on specific values using C# perform operations

I need to select data saved on the basis of different ages, design alternatives and experiences in SQL using C#. The operations are performed in the data using for loop and the loop runs for all the IDs with the specified Age, Design alternative and experience. The code is attached.
The results I am getting are the same for every age or design alternatives entered.
public partial class Form3 : Form
{
SqlCommand cnd;
SqlConnection conn = new SqlConnection(#"Data Source=HAIER-PC;Initial Catalog=c_util;Integrated Security=True");
int[] R = new int[10];
double[] W = { 4.11, 2.21, 3.42, 3.01, 2.34, 4.10,3.03, 1.18, 1.99, 5.03 };
double[] S = new double[10];
double sum;
double[] wp1= new double[10];
double[] squ= new double[10];
double[] l = new double[10];
double learn = 0.5;
int op;
int ID;
public Form3()
{
InitializeComponent();
}
private void button8_Click(object sender, EventArgs e)
{
int n;
if (conn.State != ConnectionState.Open)
conn.Open();
cnd = new SqlCommand("select * from CDes where Age='" + comboBox1.Text + "' AND Exper='" + comboBox2.Text + "'AND Design='" + comboBox3.Text + "' ", conn);
SqlDataReader myreader = cnd.ExecuteReader();
int[] R = new int[10];
while (myreader.Read())
{
string id = myreader.GetInt32(0).ToString();
string shell = myreader.GetInt32(4).ToString();
string baseg = myreader.GetInt32(5).ToString();
string vnt = myreader.GetInt32(6).ToString();
string price = myreader.GetInt32(7).ToString();
string impactliner = myreader.GetInt32(8).ToString(); ;
string eyeport = myreader.GetInt32(9).ToString();
string face = myreader.GetInt32(10).ToString();
string comfort = myreader.GetInt32(11).ToString();
string strap = myreader.GetInt32(12).ToString();
string wgt = myreader.GetInt32(13).ToString();
ID = Convert.ToInt32(id);
R[0] = Convert.ToInt32(shell);
R[1] = Convert.ToInt32(baseg);
R[2] = Convert.ToInt32(vnt);
R[3] = Convert.ToInt32(price);
R[4] = Convert.ToInt32(impactliner);
R[5] = Convert.ToInt32(eyeport);
R[6] = Convert.ToInt32(face);
R[7] = Convert.ToInt32(comfort);
R[8] = Convert.ToInt32(strap);
R[9] = Convert.ToInt32(wgt);
}
//Kohonen
for ( n = 0; n <=ID; n++)
{
S[0] = R[0] - W[0];
squ[0] = Math.Pow(S[0], 2);
S[1] = R[1] - W[1];
squ[1] = Math.Pow(S[1], 2);
S[2] = R[2] - W[2];
squ[2] = Math.Pow(S[2], 2);
S[3] = R[3] - W[3];
squ[3] = Math.Pow(S[3], 2);
S[4] = R[4] - W[4];
squ[4] = Math.Pow(S[4], 2);
S[5] = R[5] - W[5];
squ[5] = Math.Pow(S[5], 2);
S[6] = R[6] - W[6];
squ[6] = Math.Pow(S[6], 2);
S[7] = R[7] - W[7];
squ[7] = Math.Pow(S[7], 2);
S[8] = R[8] - W[8];
squ[8] = Math.Pow(S[8], 2);
S[9] = R[9] - W[9];
squ[9] = Math.Pow(S[9], 2);
sum = squ[0] + squ[1] + squ[2] + squ[3] + squ[4] + squ[5] + squ[6] + squ[7] + squ[8] + squ[9];
if (sum >= 75)
{
op = 1;
}
else
{
op = 0;
}
wp1[0] = W[0] + (learn * op * S[0]);
wp1[1] = W[1] + (learn * op * S[1]);
wp1[2] = W[2] + (learn * op * S[2]);
wp1[3] = W[3] + (learn * op * S[3]);
wp1[4] = W[4] + (learn * op * S[4]);
wp1[5] = W[5] + (learn * op * S[5]);
wp1[6] = W[6] + (learn * op * S[6]);
wp1[7] = W[7] + (learn * op * S[7]);
wp1[8] = W[8] + (learn * op * S[8]);
wp1[9] = W[9] + (learn * op * S[9]);
}
myreader.Close();
conn.Close();
textBox13.Text = wp1[0].ToString();
textBox14.Text =wp1[1].ToString();
textBox15.Text = wp1[2].ToString();
textBox20.Text = wp1[3].ToString();
textBox16.Text = wp1[4].ToString();
textBox21.Text = wp1[5].ToString();
textBox17.Text = wp1[6].ToString();
textBox22.Text = wp1[7].ToString();
textBox18.Text = wp1[8].ToString();
textBox23.Text = wp1[9].ToString();
}
I think it is caused by the you do not use the loop internal n variable, so you always read the same data. Also you overwrites R table elements in every while loop iteration.

Increase object names programmatically C#

I have a textbox for data entry and 10 textboxes for showing data. 10 viewer textboxes are visible=false by default. For example when I enter textbox count to be "3" , only 3 textboxes should be visible. (Then I can do whatever I want with those textboxes)
Here's my code;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
SqlCommand pencere = new SqlCommand("select COUNT (distinct(grup)) as ürün from fiyat", conn);
SqlCommand pencereisimleri = new SqlCommand("select distinct(grup) as ürün from fiyat", conn);
conn.Open();
SqlDataReader dr = pencere.ExecuteReader();
SqlDataAdapter da = new SqlDataAdapter(pencereisimleri);
DataTable dt1 = new DataTable();
da.Fill(dt1);
List<String> stringArr = new List<String>();
for (int a = 0; a < dt1.Rows.Count; a++)
{
TextBox[a].Visible = true;
TextBox[a].Text = "Open Textbox";
}
Not sure, if you're going for this, but how about adding the text boxes to a collection, like an array or List?
Example:
private List<TextBox> boxes = new List<TextBox>();
boxes.Add(textBox1);
boxes.Add(textBox2);
// etc...
Then you could use a foreach (or for) loop, to go through your boxes:
for (int a = 0; a < dt1.Rows.Count; a++)
{
boxes[a].Visible = true;
boxes[a].Text = "Open Textbox";
}
int count = dt1.Rows.Count;
foreach (Control x in this.Controls)
{
if (count == 0)
break;
if (x is TextBox)
{
((TextBox)x).Text = "Open Textbox";
((TextBox)x).Visible = true;
count--;
}
}
you can bind visibility to an array
Visibility[] tbViz = new Visibility[10];
public Visibility[] TbViz { get { return tbViz; } }
public void UpdateViz(int num)
{
for (int i = 0; i < tbViz.Length; i++)
{
if (i < num)
tbViz[i] = System.Windows.Visibility.Visible;
else
tbViz[i] = System.Windows.Visibility.Hidden;
}
}
Visisbility="{binding path=TbViz[0]}"
if winforms same approach
public void UpdateViz(int num)
{
for (int i = 0; i < tbViz.Length; i++)
tbViz[i].Visisble = (i < num);
}

Index was outside the bounds of the array . But All arrays are correct

Hello i'm getting "Index was outside the bounds of the array" if i'm putting value (i) in itemsWS.getItemInfo(itemLis[i].ToString())
object[] itemDetails;
object[] itemLis = itemsWS.searchItem("", "bread", "all");
int xs = 35;
int clefts = 0;
for (int i = 0; i < itemLis.Length; i++)
{
itemDetails = itemsWS.getItemInfo(itemLis[i].ToString());
Button myButtons = new Button();
myButtons.Click += delegate
{
dataGridView1.ColumnCount = 11;
dataGridView1.Columns[0].Name = "Item Code";
dataGridView1.Columns[1].Name = "Description";
dataGridView1.Columns[2].Name = "Sale Price";
dataGridView1.Columns[3].Name = "Category";
dataGridView1.Columns[4].Name = "Type";
dataGridView1.Columns[5].Name = "Status";
dataGridView1.Columns[6].Name = "Low Count";
dataGridView1.Columns[7].Name = "Medium Count";
dataGridView1.Columns[8].Name = "High Count";
dataGridView1.Columns[9].Name = "Item Picture";
dataGridView1.Columns[10].Name = "TEST";
//here is where i'm getting error if i put i on the []
itemDetails = itemsWS.getItemInfo(itemLis[i].ToString());
//
dataGridView1.Rows.Add(itemDetails);
MessageBox.Show("data grid displayed!");
};
myButtons.Text = itemDetails[1].ToString() + "\n " + itemDetails[2].ToString();
myButtons.Top = cleft * 180;
myButtons.Left = 70;
myButtons.Location = new Point(xs, clefts);
myButtons.Size = new Size(100, 60);
tabPage1.Controls.Add(myButtons);
xs += 135;
if (xs >= 537)
{
xs = 35;
clefts += 80;
}
(this is the database)
The problem is if I replace [i] with [0]. It will only display the first value item in the database
(here is the GUI if i replaced [i] with [0]) Any button clicked would repeat the same output
Please any help would be greatly appreciated. I'm trying to display items from MySQL using new buttons which was generated that would be displayed on the datagridview. Thanks
Note : that itemsWS is a web service coming from
private KPWSItems itemsWS = new KPWSItems();
Try this
for (int i = 0; i < itemLis.Length; i++)
{
itemDetails = itemsWS.getItemInfo(itemLis[i].ToString());
Button myButtons = new Button();
//myButtons.Tag = i;
myButtons.Tag = itemDetails;
myButtons.Click += (sender, args) =>
{
var button = sender as Button;
dataGridView1.ColumnCount = 11;
dataGridView1.Columns[0].Name = "Item Code";
dataGridView1.Columns[1].Name = "Description";
dataGridView1.Columns[2].Name = "Sale Price";
dataGridView1.Columns[3].Name = "Category";
dataGridView1.Columns[4].Name = "Type";
dataGridView1.Columns[5].Name = "Status";
dataGridView1.Columns[6].Name = "Low Count";
dataGridView1.Columns[7].Name = "Medium Count";
dataGridView1.Columns[8].Name = "High Count";
dataGridView1.Columns[9].Name = "Item Picture";
dataGridView1.Columns[10].Name = "TEST";
//dataGridView1.Rows.Add(itemsWS.getItemInfo(itemLis[(int)button.Tag].ToString())
dataGridView1.Rows.Add((object[])button.Tag));
MessageBox.Show("data grid displayed!");
};
myButtons.Text = itemDetails[1].ToString() + "\n " + itemDetails[2].ToString();
myButtons.Top = cleft * 180;
myButtons.Left = 70;
myButtons.Location = new Point(xs, clefts);
myButtons.Size = new Size(100, 60);
tabPage1.Controls.Add(myButtons);
xs += 135;
if (xs >= 537)
{
xs = 35;
clefts += 80;
}

dynamic object name

To minimize this code using a loop?
DataGridViewComboBoxColumn combo_1 = new DataGridViewComboBoxColumn();
string name_1 = "name_1";
string name1 = "Name 1";
combo_1.Name = name_1;
combo_1.HeaderText = name1;
DataGridViewComboBoxColumn combo_2 = new DataGridViewComboBoxColumn();
string name_2 = "name_2";
string name2 = "Name 2";
combo_2.Name = nazwa_2;
combo_2.HeaderText = nazwa2;
DataGridViewComboBoxColumn combo_3 = new DataGridViewComboBoxColumn();
string name_3 = "name_3";
string name3 = "Name_3";
combo_3.Name = name_3;
combo_3.HeaderText = name3;
DataGridViewComboBoxColumn combo_4 = new DataGridViewComboBoxColumn();
string nazwa_4 = "name_4";
string nazwa4 = "Name 4";
combo_4.Name = name_4;
combo_4.HeaderText = name4;
Sorry for not clear question, I will try to explain...
I want to add table in to datagridview1
// define combobox column
DataGridViewComboBoxColumn combo_1 = new DataGridViewComboBoxColumn();
string name_1 = "name_1";
string name1 = "Name 1";
combo_1.Name = name_1;
combo_1.HeaderText = name1;
// set value to combobox column
pol.Open();
string list_value = "SELECT value FROM table ORDER BY name ASC";
SqlCommand cmd = new SqlCommand(list_value, conn); //conn is defined above in code
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string name = rdr.GetString(0);
combo_1.Items.Add(name); /
}
pol.Close();
// add all column
dataGridView1.Columns.Add("name_column_1", "Name");
dataGridView1.Columns.Add("name_column_2", "Forname");
int index = 2;
dataGridView1.Columns.Insert(index, combo_1);
//add value to cell
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[index];
cell.Value = dt.Rows[i].ItemArray[index];
For "combo_2", "combo_3" etc. I would have to do a few of these fragments differing only in "_1"
If i can use for loop to add combo_1, combo_2, combo_3
//define combobox
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
for (int i = 1; i <= number_column; i++)
{
string name_1 = "name_" + i;
string name1 = "Name " + i;
combo_1.Name = name_1;
combo_1.HeaderText = name1;
}
//add column
for (int i = 1; i <= liczba_kolumn; i++)
{
int nr = 2;
dataGridView1.Columns.Insert(nr, combo);
nr= nr + 1;
}
//set value
for (int i = 0; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = dt.Rows[i].ItemArray[0];
dataGridView1.Rows[i].Cells[1].Value = dt.Rows[i].ItemArray[1];
for (int j = 1; j <= number_column; j++)
{
int nr = 2;
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[nr];
cell.Value = dt.Rows[i].ItemArray[nr];
nr = nr + 1;
}
}
error: Additional information: The specified column already belongs to a DataGridView control.
I know why this error is, but i do not know how to change combo name dynamicly.
I'm not sure I understand the question, but I'll take a stab at it...
Let's start with the error you have:
error: Additional information: The specified column already belongs to
a DataGridView control.
I suspect this is because of these 2 lines:
int index = 2;
dataGridView1.Columns.Insert(index, combo);
Do you perhaps mean to use the "i" from your for loop? Or maybe you meant to declare index outside of the for loop (I see you are incrementing it below)?
dataGridView1.Columns.Insert(i, combo);
or
int index = 2;
for (int i = 1; i <= number_of_columns; i++)
{
dataGridView1.Columns.Insert(index, combo);
index = index + 1; // or this could be index++;
}
Now let's look at the title and first line of your question (which is a different topic):
c# dynamic object name
To minimize this code using a loop (or other)?
You could do something like
List<DataGridViewComboBoxColumn> myColumns = new List<DataGridViewComboBoxColumn>();
for(int i=0; i < (the number you want); i++)
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.Name = "name_" + i;
combo.HeaderText = "Name " + i;
}
This would give you a (somewhat) dynamic list of DataGridViewComboBoxColumn. However, if you are just going to add them right away, you may prefer:
for(int i=0; i < (the number you want); i++)
{
dataGridView1.Columns.Insert(i, new DataGridViewComboBoxColumn{
Name = "name_" + i,
HeaderText = "Name " + i});
}
I haven't checked that for syntax, but it should at least be close.
Is that what you were going for?
Thanks PhatWrat, you helped me to solve the problem.
pol.Open();
SqlDataAdapter list_adapter = new SqlDataAdapter("SELECT value FROM box ORDER BY value ASC", pol);
DataTable list_dt = new DataTable();
list_adapter.Fill(list_dt);
string[] items = new string[list_dt.Rows.Count];
for (int i = 0; i < list_dt.Rows.Count; i++)
{
items[i] = list_dt.Rows[i][0].ToString();
//MessageBox.Show(list_dt.Rows[i][0].ToString());
}
pol.Close();
int index = 2;
for (int i = 1; i <= number_of_columns; i++)
{
dataGridView1.Columns.Insert(index, new DataGridViewComboBoxColumn
{
Name = "name_" + i,
HeaderText = "Name " + i,
DataSource = items
});
index = index + 1;
}
pol.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM table ORDER BY name ASC", pol);
DataTable dt = new DataTable();
adapter.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = dt.Rows[i].ItemArray[0];
dataGridView1.Rows[i].Cells[1].Value = dt.Rows[i].ItemArray[1];
int index_2 = 2;
for (int j = 1; j <= number_of_columns; j++)
{
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[index_2];
cell.Value = dt.Rows[i].ItemArray[index_2];
index_2 = index_2 + 1;
}
}
pol.Close();

How to use executeScalar() inside executeReader()in asp.net using C#

I am getting error :
"There is already an open DataReader associated with this Command
which must be closed first."
Code is as follows :
I am generating dynamic table using 1 table & in every row of that table I want sum calculations from another Table
I have called readDr() first using sqlcommand "cmd" & ExecuteReader(). After that I have called CalTotAmt(string CC) which uses sqlcommand "cmdTotAmt" & ExecuteScalar().
I have used 2 diff sqlcommand still its giving error.
protected void readDr()
{
string str = "select CCNo,TotalAmt,NoOfRect,Energy,New1,Theft,Misc from ChallanTable;";
cmd = new SqlCommand(str, con);
rdr = cmd.ExecuteReader();
}
protected void CreateChallanTable()
{
table.Caption = "Challan Entry";
table.ID = "Challan Entry";
table.BackColor = System.Drawing.Color.Maroon;
table.ForeColor = System.Drawing.Color.Gray;
readDr(); //call to function readDr()
Panel2.Controls.Add(table);
for (i = 0; i < 1; i++)
{
row = new TableRow();
row.BorderStyle = BorderStyle.Ridge;
m = 0;
while (rdr.Read())
{
row = new TableRow();
row.ID = "Row" + m;
row.BorderStyle = BorderStyle.Ridge;
for (n = 0; n <= 6; n++)
{
cell = new TableCell();
cell.ID = "cell" + m + n;
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
//cell.BorderColor = System.Drawing.Color.Azure;
for (n = 0; n <= 0; n++)
{
Label lbl = new Label();
lbl.ID = "lblCCRow" + m + n;
lbl.Text = Convert.ToString(rdr[0]);
lbl.Width = 70;
CCNo = lbl.Text;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (n = 1; n <= 1; n++)
{
Label lbl = new Label();
lbl.ID = "lblTotAmt" + m + n;
lbl.Text = Convert.ToString(rdr[1]);
lbl.Width = 70;
TotAmt = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
Label2.Text = Convert.ToString(CalTotAmt(CCNo));
}
for (n = 2; n <= 2; n++)
{
Label lbl = new Label();
lbl.ID = "lblNoRect" + m + n;
lbl.Text = Convert.ToString(rdr[2]);
lbl.Width = 70;
NoofRects = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (n = 2; n <= 2; n++)
{
Label lbl = new Label();
lbl.ID = "lblEnergy" + m + n;
lbl.Text = Convert.ToString(rdr[3]);
lbl.Width =70;
Energy = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (n = 3; n <= 3; n++)
{
Label lbl = new Label();
lbl.ID = "lblNew" + m + n;
lbl.Text = Convert.ToString(rdr[4]);
lbl.Width =70;
NewSD = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (n = 4; n <= 4; n++)
{
Label lbl = new Label();
lbl.ID = "lblTheft" + m + n;
lbl.Text = Convert.ToString(rdr[5]);
lbl.Width = 70;
Theft = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (n = 5; n <= 5; n++)
{
Label lbl = new Label();
lbl.ID = "lblMisc" + m + n;
lbl.Text = Convert.ToString(rdr[6]);
lbl.Width = 70;
Misc = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
//dr.NextResult();
//outer for-loop end
m++;
}
rdr.Close();
}
protected double CalTotAmt(string CC)
{
double Total = 0;
string str = "select Sum(Amount) from MainDataTable Where CC='" + CC + "' and BU ='" + LogInBU + "'";
SqlCommand cmdTotAmt = new SqlCommand(str,con);
Total = Convert.ToDouble(cmdTotAmt.ExecuteScalar());
Label2.Text = Total.ToString();
return Total;
}
Please help me out.
Here you are trying to open multiple recordset open concurrently on a same connection. You can do that by adding MultipleActiveResultSets=True to the connection string.
Try modifying you code, to make sure DataReader is closed properly:
Sample Code:
protected DataTable readDr()
{
con.Open();
string str = "select CCNo,TotalAmt,NoOfRect,Energy,New1,Theft,Misc from ChallanTable;";
cmd = new SqlCommand(str, con);
rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr);
rdr.Close();
con.Close();
}
You would generally fix it by writing a single query that computes all of the results in one go - rather than forcing your code to keep querying the database, over and over.
Something like:
select CCNo,TotalAmt,NoOfRect,Energy,New1,Theft,Misc,SumTotal
from ChallanTable ct
cross apply
(select Sum(Amount) as SumTotal from MainDataTable Where CC=ct.CCNo) t
Then you just have to process the results.
(Also, you have a bug in your display code at present - you have two attempts to do something when n==2, the second for will never be entered)

Categories

Resources