2

I just got this message from Visual Studio 2005, when I clicked on the delete command I added to my gridview using the interface for configuring the grid:

Deleting is not supported by data source 'transactionsSqlDataSource' unless DeleteCommand is specified.

How can I activate that? I want my user to be able to delete a row when they click on the button Delete.

I already changed the property DeleteCommandType of my SQLDataSource to StoredProcedure. What else do I need to do?

UPDATE

This is what I did, but I am missing something, I can't compile:

This is what I did at .cs:

public string DeleteCommand { get; set; }

How can I fix that, it says that the get is missing the body. I am not use to ASP.NET. Could you help me fixing this part of my code?

This is what I did at .aspx:

<asp:SqlDataSource ID="transactionsSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:membershipsDBConnectionString %>"
        SelectCommand="SELECT [brukerId], [kjoptekvoten], [pengeneutbetalt], [fyringsolje], [biltype], [kjoptid] FROM [Informasjon]" DeleteCommandType="StoredProcedure"
        DeleteCommand="DELETE FROM [Informasjon] WHERE fyringsolje='Kull: 2,42 kg';">
    </asp:SqlDataSource>

Is the configuration for the datasource correct? Dont worry if you see that the delete command does not use parameters like the one above, it is just for a homework(I only need the query to pass).

UPDATE 2 I keep trying but i cant fix it. I am really newbie in ASP.NET and I am not very familiar with all the terminology. What exactly is happening here? How can I know the name of my store procedure?:

CS:

public string DeleteCommand { get { return DeleteCommand; } set { DeleteCommand = "";} 

Aspx:

<asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:membershipsDBConnectionString %>"
        DeleteCommandType="StoredProcedure" SelectCommand="SELECT [id], [kjoptekvoten], [pengeneutbetalt], [fyringsolje], [biltype], [kjoptid] FROM [Informasjon]"
        DeleteCommand="StoredProcedureName">
    </asp:SqlDataSource>

When I click on Delete that row needs to be erased from table Informasjon Screenshot of GridView

Rob
  • 45,296
  • 24
  • 122
  • 150
javing
  • 12,307
  • 35
  • 138
  • 211

2 Answers2

5
DeleteCommand="<your stored proc>"

is needed in your SqlDataSource along with any parameters you want, for instance:

<DeleteParameters>
  <asp:Parameter Name="myParameter" Type="Int32" />
</DeleteParameters>

which needs to be added in the SqlDatasource tag.

Crypth
  • 1,576
  • 18
  • 32
  • What is missing? Do i need to have does parametters? can i make it work with out them? See update. – javing May 09 '11 at 10:37
  • No there is no need to use parameters unless you want them and there's no need for a DeleteCommand string in your .cs file. What is the compiler error? – Crypth May 09 '11 at 10:48
2

You can either call a stored procedure if you have created one like below :

<asp:SqlDataSource ID="SqlDataSource5" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    // in the select command or your case delete you just input the name instead of a query
    SelectCommand="SELECT * FROM [Table] WHERE [Column1]=@Parameter1 AND [Column2]=@Parameter2 AND [Column3]=@Parameter3;" 
    DeleteCommand="StoredProcedureName"
    // Then ofcourse put the commandtype on storedprocedure but you already have that
    DeleteCommandType="StoredProcedure">
    // And you can declare the parameters like below
    <SelectParameters>
        <asp:Parameter Name="Parameter1" Type="String" />
        <asp:Parameter Name="Parameter2" Type="Boolean" />
        <asp:Parameter Name="Parameter3" Type="String" />
        <asp:Parameter Name="Parameter4" Type="Boolean" />
    </SelectParameters>
    <DeleteParameters>
         <asp:Parameter Name="Parameter1" Type="String" />
    </DeleteParameters>
</asp:SqlDataSource>

For more information about storedprocedures please read : What is a stored procedure? Or you can directly call a query like in the example below :

<asp:SqlDataSource ID="SqlDataSource5" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [informasjon];"
    DeleteCommand="DELETE FROM [informasjon] WHERE [id]=@id;">
    <DeleteParameters>
         <asp:Parameter Name="id" Type="Int" />
    </DeleteParameters>
</asp:SqlDataSource>

The connection string is something you will have to provide for yourself. For more info on the connectionstring please read : https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring%28v=vs.110%29.aspx

Community
  • 1
  • 1
Ruben
  • 699
  • 2
  • 16
  • 36
  • Sorry but i still confused. this is how my code currently looks like: ` ` I need to do something similar tho what you said, but just for deleting. But how? – javing May 09 '11 at 11:27
  • If you add the tag DeleteCommand="StoredProcedureName" It will use the specified storedprodedure. And all the parameters that your stored procedure needs should aslo be added like i did in the example above – Ruben May 09 '11 at 11:29
  • I don't know what is the name of my StoreProcedure. Where can i check that? i don't find that anywhere. – javing May 09 '11 at 11:35
  • If you make your stored procedure in sql you will have to give it a name well that is the name you can also find the name in the sql server(manager) http://msdn.microsoft.com/en-us/library/ms190669(v=SQL.100).aspx – Ruben May 09 '11 at 11:37
  • But are you really sure you want to use a stored procedure? – Ruben May 09 '11 at 11:40
  • I am not sure, i think i dont know what the storeProcedureIs. I am really noob in ASP.NET and my homework says that i need to delete the row when i click on the button. How do you think i should do this in the easiest way. Thanks for your help. – javing May 09 '11 at 11:51
  • You do have to use a sql server? It is probably easier without stored procedure if you look at my example i made the select statement a normal statement instead of a stored procedure. – Ruben May 09 '11 at 11:53
  • So i need to write the delete query where it says Delete Command? SOmething like this?: `DeleteCommand="DELETE FROM [Informasjon] WHERE id=selected row">` Could you please help me complete this query? – javing May 09 '11 at 11:55
  • Yea something like that. If you look at my example i added a new piece of code that will be allot like what you need. Also i think you use it with a gridview? you have bound the sqldatasource to the gridview? Like this? DataSourceID="SqlDataSource4" inside the gridview tag – Ruben May 09 '11 at 12:01
  • 2
    You should do some research on what a stored procedure is and not wait on the professor to teach you. – Security Hound May 09 '11 at 12:30