dazza12345
Programmer
Hi all,
i have the following ObjectDataSource and GridView defined on my webpage:
I have the ObjectDataSource configured to Select and Delete methods of a data function. below are the select amnd delete functions:
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
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