Reputation: 3
I want to use each listbox1 item to run the both query and if both result are not same than move that item to another listbox called listbox2 if they are same than delete that item from listbox1.
foreach (string Items in listBox1.Items)
{
using (OracleCommand crtCommand = new OracleCommand("select count(*) from(( select * from all_ind_columns where index_name= '" + Items + "' and table_owner='" + txtSrcUserID.Text.ToUpper() + "'))", conn1))
using (OracleCommand ctCommand = new OracleCommand("select count(*) from(( select * from all_ind_columns where index_name= '" + Items + "' and table_owner='" + txtDesUserID.Text.ToUpper() + "'))", conn1))
{
string result1 = crtCommand.ExecuteScalar().ToString();
string result2 = ctCommand.ExecuteScalar().ToString();
if (result1 != result2)
{
//move that item to listbox2
}
else if(result1 == result2)
{
// remove that item from listbox1
}
}
}
Upvotes: 0
Views: 604
Reputation: 63065
You cant use foreach
here because you change the listBox1.Items
inside the loop,
Use while loop and check the listBox1.Items.Count() >0
and inside the loop you can pic first item and move it to second one or remove.
while (ListBox1.Items.Count>0)
{
var item = ListBox1.Items[0].ToString();
using (OracleCommand crtCommand = new OracleCommand("select count(*) from(( select * from all_ind_columns where index_name= '" + item + "' and table_owner='" + txtSrcUserID.Text.ToUpper() + "'))", conn1))
using (OracleCommand ctCommand = new OracleCommand("select count(*) from(( select * from all_ind_columns where index_name= '" + item + "' and table_owner='" + txtDesUserID.Text.ToUpper() + "'))", conn1))
{
string result1 = crtCommand.ExecuteScalar().ToString();
string result2 = ctCommand.ExecuteScalar().ToString();
if (result1 != result2)
{
ListBox2.Items.Add(item);
}
ListBox1.Items.RemoveAt(0);
}
}
Note: your code is open for sql injection attacks, use parameters instead of inline parameters.
while (ListBox1.Items.Count>0)
{
var item = ListBox1.Items[0].ToString();
using (OracleConnection con = new OracleConnection(connectionString))
using (OracleCommand cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = "select count(*) from(( select * from all_ind_columns where index_name= :item and table_owner=:table_owner))";
cmd.Parameters.Add(item);
cmd.Parameters.Add(txtSrcUserID.Text.ToUpper());
string result1 = cmd.ExecuteScalar().ToString();
cmd.Parameters.Clear();
cmd.Parameters.Add(item);
cmd.Parameters.Add(txtDesUserID.Text.ToUpper());
string result2 = cmd.ExecuteScalar().ToString();
if (result1 != result2)
{
ListBox2.Items.Add(item);
}
ListBox1.Items.RemoveAt(0);
}
}
Upvotes: 2