Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems deleting from gridview

Status
Not open for further replies.

dazza12345

Programmer
Nov 25, 2005
35
GB
Hi all,

i have the following ObjectDataSource and GridView defined on my webpage:

Code:
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="DeleteStockExchangeDetails"
        SelectMethod="GetAllStockExchangeDetails" TypeName="StockDetails">
        <DeleteParameters>
            <asp:Parameter Name="UserID" Type="Int16" />
            <asp:Parameter Name="StockExchangeID" Type="Int16" />
        </DeleteParameters>
    </asp:ObjectDataSource>
    
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" DataSourceID="ObjectDataSource1"
        ForeColor="#333333" GridLines="None">
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
        <RowStyle BackColor="#EFF3FB" />
        <EditRowStyle BackColor="#2461BF" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>

I have the ObjectDataSource configured to Select and Delete methods of a data function. below are the select amnd delete functions:

Code:
    public static DataTable GetAllStockExchangeDetails()
    {
        DataTable Stocktable = new DataTable("Stocktable");
        Stocktable.Columns.Add("StockExchangeID");
        Stocktable.Columns.Add("UserID");
        Stocktable.Columns.Add("CompanyName");
        Stocktable.Columns.Add("Symbol");
        Stocktable.Columns.Add("market");
        Stocktable.Columns.Add("PurchaseDate");
        Stocktable.Columns.Add("PurchasePrice");
        Stocktable.Columns.Add("Quantity");
        Stocktable.Columns.Add("CurrentPrice");
        Stocktable.Columns.Add("CurrentValue");
        Stocktable.Columns.Add("Nominee");

        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = connection;
                command.CommandText = "SELECT * FROM Stock";

                // WHERE UserID = 1
                //command.Parameters.AddWithValue("@UserID", UserID);

                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    Stocktable.Rows.Add(reader["StockExchangeID"], reader["UserID"], reader["CompanyName"], reader["Symbol"], reader["market"], reader["PurchaseDate"], reader["PurchasePrice"], reader["Quantity"], reader["CurrentPrice"], reader["CurrentValue"], reader["Nominee"]);
                }


                connection.Close();
            }
        }
        return Stocktable;
    }

    public static void DeleteStockExchangeDetails(short UserID, short StockExchangeID)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = connection;
                command.CommandText = "DELETE FROM Stock " +
                    "WHERE UserID = @UserID AND StockExchangeID = @StockExchangeID";

                command.Parameters.AddWithValue("@UserID", UserID);
                command.Parameters.AddWithValue("@StockExchangeID", StockExchangeID);

                connection.Open();

                command.ExecuteNonQuery();

                connection.Close();
            }
        }
    }

Eveerthing works fine until I click the 'Delete' button on a row in the gridview. Once I do this an error accurs. It is caused by no parameters being passed into the Delete function (userid and StockExchangeID), the values that are being passed to this function is 0,0.

How would I go about getting the correct values for the parameters from the row in the gridview?

Cheers
 
Im trying to use the GridView.RowCommand that you suggested, however, at present all ive done is add the following into the defaulst.aspx.cs:

Code:
    void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        // Convert the row index stored in the CommandArgument
        // property to an Integer.
        int index = Convert.ToInt32(e.CommandArgument);
    }

And the following into the definition og the gridview:

Code:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" DataSourceID="ObjectDataSource1"
        ForeColor="#333333" GridLines="None" onrowcommand="GridView1_RowCommand">

Even this does not woant to work. When running the program, I get the following error:

Compiler Error Message: CS0122: 'Default3.GridView1_RowCommand(object, System.Web.UI.WebControls.GridViewCommandEventArgs)' is inaccessible due to its protection level

Any ideas how i can get rid of this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top