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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting from a gridview

Status
Not open for further replies.

wrexhamafc234

Programmer
Oct 23, 2006
18
GB
I have the following gridview defined on my page:

Code:
        <asp:GridView ID="GridView1" runat="server" CellPadding="3" DataSourceID="ObjectDataSource1" GridLines="Vertical" onrowcommand="GridView1_RowCommand" AllowSorting="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" Width="700px">
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="Gainsboro" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            </Columns>
        </asp:GridView>

Whenever the 'Delete' button on a row is clicked, the following fucntion will be called:

Code:
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            int TableID = Convert.ToInt32(e.CommandArgument);
        }
        else if (e.CommandName == "Edit")
        {

        }
    }

This is where my problem begins. In order for me to delete a row, I will be requiring to retrive the TableID. However, each and every time, TableID will be returned as 0 (zero). Why am I not getting any values within e.CommandArgument?
 
Try setting the datakeynames property to the primary key field


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Ah, right. Ive added the datakeynames to the Gridview definition.

Code:
 <asp:GridView ID="GridView1" runat="server" CellPadding="3" DataSourceID="ObjectDataSource1" datakeynames="StockExchangeID" GridLines="Vertical" onrowcommand="GridView1_RowCommand" AllowSorting="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" Width="700px">

However, e.CommandArgument is still coming out as zero. Any ideas?

Does it matter that the ObjectDataSource that im using to populate the GridView is returning a Datatable?

Code:
public static DataTable GetAllStockExchangeDetails(short UserID)
    {
        DataTable Stocktable = new DataTable("Stocktable");
        Stocktable.Columns.Add("StockExchangeID");
        Stocktable.Columns.Add("Company Name");
        Stocktable.Columns.Add("Symbol");
        Stocktable.Columns.Add("Market");
        Stocktable.Columns.Add("Purchase Date");
        Stocktable.Columns.Add("Currency");
        Stocktable.Columns.Add("Purchase Price");
        Stocktable.Columns.Add("Quantity");
        Stocktable.Columns.Add("Current Price");
        Stocktable.Columns.Add("Current Value");
        Stocktable.Columns.Add("Nominee and custody arangements");

        object CompanyName = null;
        object Symbol = null;
        object Market = null;
        object PurchaseDate = null;
        object Currency = null;
        object PurchasePrice = null;
        object Quantity = null;
        object CurrentPrice = null;
        object CurrentValue = null;
        object Nominee = null;
        DateTime pdate;

        //Encrypting, beofre placing values into database
        EncryptionModule encryptor4 = new EncryptionModule("Test", "Test", 1, Encryption.Username, Encryption.PrivateCertificate, Encryption.CertificatePassword);

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

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

                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    
                    CompanyName = encryptor4.DecodeString((byte[])reader["CompanyName"]);
                    Symbol = encryptor4.DecodeString((byte[])reader["Symbol"]);
                    Market = encryptor4.DecodeString((byte[])reader["Market"]);
                    PurchaseDate = encryptor4.DecodeDate((byte[])reader["PurchaseDate"]);
                    Currency = encryptor4.DecodeString((byte[])reader["Currency"]);
                    PurchasePrice = encryptor4.DecodeString((byte[])reader["PurchasePrice"]);
                    Quantity = encryptor4.DecodeString((byte[])reader["Quantity"]);
                    CurrentPrice = encryptor4.DecodeString((byte[])reader["CurrentPrice"]);
                    CurrentValue = encryptor4.DecodeString((byte[])reader["CurrentValue"]);
                    pdate = (DateTime)PurchaseDate;

                    Nominee = encryptor4.DecodeString((byte[])reader["Nominee"]);
                    //reader["StockExchangeID"], reader["UserID"],
                    Stocktable.Rows.Add((short)reader["StockExchangeID"], (string)CompanyName, (string)Symbol, (string)Market, pdate.ToShortDateString(), (string)Currency, (string)PurchasePrice, (string)Quantity, (string)CurrentPrice, (string)CurrentValue, (string)Nominee);
                }

                connection.Close();
            }
        }
        return Stocktable;
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top