Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
C#
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
Updated 23-Dec-12 20:15pm
v6
Comments
[no name] 24-Dec-12 1:57am    
What is po over here ?
Orcun Iyigun 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 24-Dec-12 2:00am    
that i am checking the condition that column containing zero
po is equal to zero
ravuravu 24-Dec-12 2:02am    
suppose i gave the condition if(ui.Columns[0][0]==0)

i got errorr
__TR__ 24-Dec-12 2:05am    
What's the error message?

Try this:
C#
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
 
Share this answer
 
Your SQL query will be like as,

SQL
DELETE FROM Items WHERE Quantity=0
 
Share this answer
 
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:
SQL
"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:

C#
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.
 
Share this answer
 
v3
Comments
Anuja Pawar Indore 24-Dec-12 6:49am    
Added code block
Zafar Sultan 24-Dec-12 6:57am    
Thanks.
C#
 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..
 
Share this answer
 
Comments
ravuravu 24-Dec-12 2:17am    
but its for null case i need it for zero value case
ravuravu 24-Dec-12 2:18am    
i want to delete the row when the Quantity equal to zero
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900