Hi all, ive got the follwing gridview defined within my aspx page:
Ive set the datakeynames to "StockExchageID" which is the primary key of the database where the data for the gridview is coming from. The gridview is populated using an ObjectDataSource, The SELECT method of the ObejctDataSource is the following datafunction: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">
When the delete button is clicked (the delete button from the gridview), the following function is executed: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; 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 = (string)reader["CompanyName"]); Symbol = (string)reader["Symbol"]); Market = (string)reader["Market"]); PurchaseDate = (DateTime)reader["PurchaseDate"]); Currency = (string)reader["Currency"]); PurchasePrice = (string)reader["PurchasePrice"]); Quantity = (string)reader["Quantity"]); CurrentPrice = (string)reader["CurrentPrice"]); CurrentValue = (string)reader["CurrentValue"]); pdate = (DateTime)PurchaseDate; Nominee = (string)reader["Nominee"]); 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; }
Each and every time it is executed, TableId will be returned as zero. Why cant I get the StockExchangeID from the database to be displayed?Code:protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Delete") { int TableID = Convert.ToInt32(e.CommandArgument); } else if (e.CommandName == "Edit") { } }
Thanks




Bookmarks