Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to delete a row from datagrid and datatable. So I wrote the following process to be done.
Now my error is that the Scalar variable is not declared. I literally don't know how to fix this

What I have tried:

C#
int selectedIndex = datagriduser.SelectedIndex;
               DataRowView rowview = datagriduser.SelectedItem as DataRowView;
               //  string cltyp = rowview["ClientTyp"].ToString();
               var cltyp = "";
               var macadress = "";
               var userkenn = "";
                // OleDbCommand olecmd = new OleDbCommand("DELETE From AktiveUser WHERE User_Kennzeichen = 'nh'", conn);
               string query = "DELETE From AktiveUser WHERE ClientTyp= @cltyp AND MAC_Adresse =  @macadress AND User_Kennzeichen = @userkenn";
               OleDbCommand olecmd = new OleDbCommand(query, conn);
              // olecmd.ExecuteNonQuery();
               // OleDbCommand olecmd = new OleDbCommand("DELETE From AktiveUser WHERE User_Kennzeichen = 'nh'", conn);
               olecmd.Parameters.AddWithValue(@cltyp, rowview["ClientTyp"].ToString());
               olecmd.Parameters.AddWithValue(@macadress, rowview["Arbeitsplatz"].ToString());
               olecmd.Parameters.AddWithValue(@userkenn, rowview["Anmeldung"].ToString());
               if (selectedIndex != -1)
               {
                   var deletedrow = dbtable.Rows[selectedIndex];
                   deletedrow.Delete();
                   oleda.DeleteCommand = olecmd;
                   oleda.Update(dbtable);
               }
               else
               {
                   System.Windows.MessageBox.Show("Please Select a row to Delete");
               }
Posted
Updated 20-Feb-19 3:57am

You have two mistakes here; the first is that the parameters being added need to be within double quotes (as the prior answers have pointed out).

The other is that the OleDB database provider does not support named parameters within the query itself.
OleDbCommand.Parameters Property (System.Data.OleDb) | Microsoft Docs[^]
OleDB Documentation
The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used.


Try these changes to your problem
C#
string query = "DELETE From AktiveUser WHERE ClientTyp= ? AND MAC_Adresse =  ? AND User_Kennzeichen = ?";
OleDbCommand olecmd = new OleDbCommand(query, conn);

olecmd.Parameters.AddWithValue("@cltyp", rowview["ClientTyp"].ToString());
olecmd.Parameters.AddWithValue("@macadress", rowview["Arbeitsplatz"].ToString());
olecmd.Parameters.AddWithValue("@userkenn", rowview["Anmeldung"].ToString());
 
Share this answer
 
Comments
Priya Karthish 20-Feb-19 10:29am    
Thanks. It really worked well. I was hitting my head for the whole day
MadMyche 20-Feb-19 11:29am    
You're welcome
You haven't put quotes around @cltyp and the two others, so now it just refers to the value of the cltyp variable - which is an empty string. Put quotes around @cltyp, @macadress and @userkenn.
 
Share this answer
 
Comments
Priya Karthish 20-Feb-19 8:28am    
singlequote rite
Priya Karthish 20-Feb-19 8:32am    
I tried putting both singlequote and doublequote. It's not working still
Richard Deeming 20-Feb-19 9:53am    
Not in the query - in the AddWithValue lines.

What you've got at the moment is effectively:
olecmd.Parameters.AddWithValue("", rowview["ClientTyp"].ToString());

It needs to be:
olecmd.Parameters.AddWithValue("@cltyp", rowview["ClientTyp"].ToString());

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