Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a List named rowlist

C#
if (dataGridView1.SelectedRows.Count > 1)
            {
                List<string> rowList = new List<string>();

                foreach (DataGridViewRow r in dataGridView1.SelectedRows)
                    if (r.Cells[3].Value.ToString() != null)
                        rowList.Add(r.Cells[3].Value.ToString());
}

then I want to delete database with sqlcommand based on this list.

C#
commandtext ="delete mytable where recordid in(select ???)
i dont know input after select.
please help me.
Posted
Updated 25-Aug-13 22:28pm
v2
Comments
CodeBlack 26-Aug-13 4:50am    
what do you want insetead of select ? data from rowList ?
CodeBlack 26-Aug-13 5:18am    
i have provided solution for this. and i doubt with your delete command query. i think it is missing 'from' keyword. so your delete query should be like this
"delete from mytable where recordid in (...)"

I am not sure about MySQL but if you are using MSSQL then "from" keyword should be there.
Member 10222112 26-Aug-13 5:28am    
i have problem with code in (..)
i dont know to input things in (..)
CodeBlack 26-Aug-13 5:29am    
Did you check my solution which I have provided ?
Member 10222112 26-Aug-13 5:33am    
where?

See below :

C#
commandtext = "delete mytable where recordid in(" + string.Join(",", rowList.ToList()) + ")"
 
Share this answer
 
v2
Using Sql Delete Command directly in FE is not a good idea. I would suggest you to create a procedure to delete the selected row. As you are storing the selected rows ID in list<string></string>, pass the list to the procedure(See here : C# SQL Server - Passing a list to a stored procedure[^].) and perform the delete operation in procedure with Sql Transaction.

Please see : What are the pros and cons to keeping SQL in Stored Procs versus Code[^].


--Amit
 
Share this answer
 
try this :-
Make string of data from rowlist and add into your delete query

string data= null;
if (dataGridView1.SelectedRows.Count > 1)
            {
                List<string> rowList = new List<string>();
 
                foreach (DataGridViewRow r in dataGridView1.SelectedRows)
		  {
                    if (r.Cells[3].Value.ToString() != null)
                        {
                          rowList.Add(r.Cells[3].Value.ToString());
			  data = data + "'" + r.Cells[3].Value.ToString() + "',";
                        }
		  }
}
if (Strings.Len(data) >= 1) {
		data = Strings.Mid(data, 1, Strings.Len(data) - 1);
}
commandtext = "delete mytable where recordid in(" + data + ")";
 
Share this answer
 
Your input query must be in this format :
DELETE FROM dbo.mytable WHERE recordid IN('Value 1','Value 2','Value 3')


//So modify the below command it will work
commandtext = "delete mytable where recordid in(" + string.Join(",", rowList.ToList()) + ")"

Thank you.
 
Share this answer
 
C#
if (dataGridView1.SelectedRows.Count > 1)
 {
    string strValue=String.Empty;

    foreach (DataGridViewRow r in dataGridView1.SelectedRows)
     {
      if (r.Cells[3].Value.ToString() != null)
        {
          strValue+=(r.Cells[3].Value.ToString())+',';
        }
     }
}


And then you use

C#
commandtext ="delete mytable where recordid in(strValue)


Thanks & Regard
Sham :)
 
Share this answer
 
Hello,

try this.

C#
commandtext ="delete mytable where recordid in("+String.Join(',',rowList.ToArray())+")";
 
Share this answer
 

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