Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
I have Four fields in my table Item,Quantity,Price,Date
my table name is Items
 
Item Quantity Price Date
..............................
Maida 9 7 12/24/2012
onion 12 8 12/24/2012
salt 0 0 12/24/2012
oil 6 4 12/24/2012
 
i want to delete the raw that containing Quantity equal to zero.
i want to delete the raw data from database
string po = "0";
        string j = "select Quantity from Item";
        SqlCommand vvn = new SqlCommand(j, DbConnection.mCon);
        SqlDataAdapter lk = new SqlDataAdapter(vvn);
        DataTable ui = new DataTable();
        lk.Fill(ui);
        if (ui.Columns[0][0].ToString()==po)
        {
            string d6 = "delete from Item where Item='" + TextBox1.Text + "' and Quantity='" + TextBox2.Text + "' and Price='" + TextBox3.Text + "' and Date='" + TextBox4.Text + "'";
            SqlCommand cnm6 = new SqlCommand(d6, DbConnection.mCon);
            cnm6.ExecuteNonQuery();
        }
 
Pls Correct it The mistake occur in if (ui.Columns[0][0].ToString()==po) this statement pls help me
Posted 23-Dec-12 20:55pm
Edited 23-Dec-12 21:15pm
v6
Comments
Krunal Rohit at 24-Dec-12 1:57am
   
What is po over here ?
Orcun Iyigun at 24-Dec-12 4:29am
   
OP;that i am checking the condition that column containing zero
po is equal to zero
suppose i gave the condition if(ui.Columns[0][0]==0)
 
i got errorr
ravuravu at 24-Dec-12 2:00am
   
that i am checking the condition that column containing zero
po is equal to zero
ravuravu at 24-Dec-12 2:02am
   
suppose i gave the condition if(ui.Columns[0][0]==0)
 
i got errorr
__TR__ at 24-Dec-12 2:05am
   
What's the error message?
Orcun Iyigun at 24-Dec-12 4:29am
   
nothing ,no error message ,when i compile it, it Exiting from if loop it doesn't go to if loop
ravuravu at 24-Dec-12 2:12am
   
nothing ,no error message ,when i compile it, it Exiting from if loop it doesn't go to if loop
Orcun Iyigun at 24-Dec-12 4:30am
   
When you comment try the reply button... otherwise the person who asked you the question will not be notified.
__TR__ at 24-Dec-12 4:42am
   
Check the value of ui.Columns[0][0].ToString() in debug mode. Looks like the expression of If statement is not true, hence your code is not entering the block inside If statement.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this:
string po = "0";
string j = "select Quantity from Item";
SqlCommand vvn = new SqlCommand(j, DbConnection.mCon);
SqlDataReader lk =vvn.ExecuteReader();
while(lk.Read()){
    if (lk[0].ToString()==po)
    {
        string d6 = "delete from Item where Item='" + TextBox1.Text + "' and Quantity='" + TextBox2.Text + "' and Price='" + TextBox3.Text + "' and Date='" + TextBox4.Text + "'";
        SqlCommand cnm6 = new SqlCommand(d6, DbConnection.mCon);
        cnm6.ExecuteNonQuery();
    }
}
 
This may help you.
 
--Amit
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Your SQL query will be like as,
 
DELETE FROM Items WHERE Quantity=0
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Why do you need to do it this way only? As you are already passing Item, Quantity, Price and Date you can do that in a single query. For example:
"delete from Item where Item='" + TextBox1.Text + "' and Quantity='" + TextBox2.Text + "' and Price='" + TextBox3.Text + "' and Date='" + TextBox4.Text + "'" +" and Quantity = 0";
 
Also, I am not sure this is going to work as you are comparing date which at times can be quite tricky to deal with. And, if you want it in your way only try this:
 
string po = "0";
        string j = "select Quantity from Item";
        SqlCommand vvn = new SqlCommand(j, DbConnection.mCon);
        SqlDataAdapter lk = new SqlDataAdapter(vvn);
        DataTable ui = new DataTable();
        lk.Fill(ui);
for(int i = 0; i < ui.Rows.Count; i++)
{
        if (ui.Rows[i][0].ToString().Trim()==po)
        {
            string d6 = "delete from Item where Item='" + TextBox1.Text + "' and Quantity='" + TextBox2.Text + "' and Price='" + TextBox3.Text + "' and Date='" + TextBox4.Text + "'";
            SqlCommand cnm6 = new SqlCommand(d6, DbConnection.mCon);
            cnm6.ExecuteNonQuery();
        }
}
This might not work also as suppose the quantity of first record is "0" but the item,price and Date does not match the passed parameters. You will have to do it the "other" way. Think of it and share with us. We will definitely help you out.
  Permalink  
v3
Comments
Anuja Pawar Indore at 24-Dec-12 6:49am
   
Added code block
zafar sultan at 24-Dec-12 6:57am
   
Thanks.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

 string j = "select Quantity from Item";
        SqlCommand vvn = new SqlCommand(j, DbConnection.mCon);
        SqlDataAdapter lk = new SqlDataAdapter(vvn);
        DataTable ui = new DataTable();
        lk.Fill(ui);
foreach(DataRow dr in ui.Rows)
{
    if(dr == null)
    {
        string d6 = "delete from Item where Item='" + TextBox1.Text + "' and Quantity='" + TextBox2.Text + "' and Price='" + TextBox3.Text + "' and Date='" + TextBox4.Text + "'";
            SqlCommand cnm6 = new SqlCommand(d6, DbConnection.mCon);
            cnm6.ExecuteNonQuery();
    }
}
 
Its my blind assumption, hope it will solve your issue..
  Permalink  
Comments
ravuravu at 24-Dec-12 2:17am
   
but its for null case i need it for zero value case
ravuravu at 24-Dec-12 2:18am
   
i want to delete the row when the Quantity equal to zero
Krunal Rohit at 24-Dec-12 2:20am
   
try, if(dr == po)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 335
1 OriginalGriff 250
2 DamithSL 170
3 Kornfeld Eliyahu Peter 130
4 Peter Leow 95
0 OriginalGriff 7,315
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,917
3 Maciej Los 4,866
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100