Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

c# - i got "Missing operand after 'Id' operator." error, what happen in my code friends?

case 2:
    int intRemoveOption;
    Console.WriteLine("Select You Want to Delete Entire Column or Seperate:
");
    Console.WriteLine("Select 11. for Remove Entire Column");
    Console.WriteLine("Select 12. For Remove Single Column
");

    intRemoveOption = Convert.ToInt32(Console.ReadLine());
    if (intRemoveOption ==11)
    {                         
        Console.WriteLine("Enter Id");                            
        objdatarowcollection = objDataTable.Select("student Id =");
        if (objdatarowcollection != null && objdatarowcollection.Length > 0)
            objdatarowcollection[0].Delete();
        Console.WriteLine("number of rows 
{0}", objDataTable.Rows.Count.ToString());                            
        Console.WriteLine("The Record is Deleted Now");
        datarow = objDataTable.Select();
    foreach (DataRow datarw in datarow)

        foreach (DataColumn datacl in datarw.Table.Columns)

            Console.WriteLine(" {0}", datarw[datacl]);

I tried lot but I can't, friends help this code, how to remove column?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This line is wrong

objdatarowcollection = objDataTable.Select("student Id =");

First, a field name with spaces requires square brackets around it.
Second, the Select method expects a syntax like you use in the sql WHERE statements. Thus, after the equals, you need a value to filter the row collection

 objdatarowcollection = objDataTable.Select("[student Id] =" + someValueToSearchFor);

Said that, your question is still unclear. What do you mean with "Remove Column"? You remove a column removing it from the DataTable in which it exists

For example:

  objDataTable.Columns.Remove("ColumnName");

Instead your code tries to delete the row at index 0 of the returned collection of row from the Select method, so if you want to remove a column:

objDataTable.Columns.Remove("C2");
+------+-----+------+                              +------+------+
|  c1  |  c2 |  c3  |                              |  c1  |  c3  |
+------+-----+------+                              +------+------+
|   A  |  D  |  G   |    Remove a column named c2  |   A  |  G   |
+------+-----+------+          result is           +------+------+ 
|   B  |  E  |  H   |                              |   B  |  H   |
+------+-----+------+                              +------+------+ 
|   C  |  F  |  I   |                              |   C  |  I   |
+------+-----+------+                              +------+------+ 

if you want to delete a row at index 0 of the returned collection (Notice that if the value to search for is a string you need quotes around the value)

objdatarowcollection = objDataTable.Select("C3 ='H'");
objdatarowcollection[0].Delete();
+------+-----+------+                              +------+-----+------+
|  c1  |  c2 |  c3  |                              |  c1  |  c2 |  c3  |
+------+-----+------+                              +------+-----+------+
|   A  |  D  |  G   |    Delete a ROW with value   |   A  |  D  |   G  |
+------+-----+------+  'H' in column c3 results in +------+-----+------+
|   B  |  E  |  H   |                              |   C  |  F  |   I  |
+------+-----+------+                              +------+-----+------+
|   C  |  F  |  I   |                              
+------+-----+------+                              

Keep in mind that here the words "remove/delete" are virtual.
The DataTable is an in memory object. Deleting elements from the in-memory row collection or removing from the column collection doesn't delete/remove anything in the database physical structure. For that you need real sql queries and the support of the ADO.NET provider of your database engine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...