C
cmrchs
Hi,
I'm trying out Databinding to a Data Acces Layer using a
ObjectDataSource-control
The Update works fine but the Delete-method doesn't. when debugging I
see that my productID-parameter is always 0 and not the ID of the
selected record (which is the case when updating)
How come?
Here's the code of the GridView and ObjectDataSource-control:
<asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1"
AutoGenerateEditButton="True"
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1"
runat="server" TypeName="ProductsDB"
SelectMethod="SelectProducts"
UpdateMethod="UpdateProduct"
DeleteMethod="DeleteProduct">
<UpdateParameters>
<asparameter Name="productID" Type="Int32" />
<asparameter Name="productName" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asparameter Name="productID" Type="Int32" />
</DeleteParameters>
</asp:ObjectDataSource>
And the code of the class I bind to:
public class ProductsDB
{
// Data Members
private static IDbConnection _dbConnection;
// Ctor
static ProductsDB()
{
string connectionString =
ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
_dbConnection = new SqlConnection(connectionString);
}
// Methods
public static DataSet SelectProducts()
{
string queryString = "SELECT [ProductID], [ProductName]FROM
[Products]";
IDbCommand dbCommand = new SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = _dbConnection;
IDbDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = dbCommand;
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
} // SelectProducts()
public static int UpdateProduct(int productID, string productName,
decimal unitPrice, short unitsInStock, bool discontinued)
{
string queryString = "UPDATE [Products] SET [ProductName] =
@ProductName WHERE [ProductID] = @ProductID";
IDbCommand dbUpdateCommand = new SqlCommand();
dbUpdateCommand.CommandText = queryString;
dbUpdateCommand.Connection = _dbConnection;
IDataParameter dbParam = new SqlParameter();
dbParam.ParameterName = "@ProductID";
dbParam.Value = productID;
dbParam.DbType = DbType.Int32;
dbUpdateCommand.Parameters.Add(dbParam);
dbParam = new SqlParameter();
dbParam.ParameterName = "@ProductName";
dbParam.Value = productName;
dbParam.DbType = DbType.String;
dbCommand.Parameters.Add(dbParam);
int rowsAffected = 0;
_dbConnection.Open();
try
{
rowsAffected = dbUpdateCommand.ExecuteNonQuery();
}
finally
{
_dbConnection.Close();
}
return rowsAffected;
} // UpdateProduct()
public static int DeleteProduct(int productID) // ERROR:
'productID' is always 0 ???
{
string queryString = "DELETE FROM [Products] WHERE [ProductID] =
@ProductID";
IDbCommand dbDeleteCommand = new SqlCommand();
dbDeleteCommand.CommandText = queryString;
dbDeleteCommand.Connection = _dbConnection;
IDataParameter dbParam = new SqlParameter();
dbParam.ParameterName = "@ProductID";
dbParam.Value = productID;
dbParam.DbType = DbType.Int32;
dbDeleteCommand.Parameters.Add(dbParam);
int rowsAffected = 0;
_dbConnection.Open();
try
{
rowsAffected = dbDeleteCommand.ExecuteNonQuery();
}
finally
{
_dbConnection.Close();
}
return rowsAffected;
} // DeleteProduct()
} // class ProductsDB
Thank you
Chris
I'm trying out Databinding to a Data Acces Layer using a
ObjectDataSource-control
The Update works fine but the Delete-method doesn't. when debugging I
see that my productID-parameter is always 0 and not the ID of the
selected record (which is the case when updating)
How come?
Here's the code of the GridView and ObjectDataSource-control:
<asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1"
AutoGenerateEditButton="True"
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1"
runat="server" TypeName="ProductsDB"
SelectMethod="SelectProducts"
UpdateMethod="UpdateProduct"
DeleteMethod="DeleteProduct">
<UpdateParameters>
<asparameter Name="productID" Type="Int32" />
<asparameter Name="productName" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asparameter Name="productID" Type="Int32" />
</DeleteParameters>
</asp:ObjectDataSource>
And the code of the class I bind to:
public class ProductsDB
{
// Data Members
private static IDbConnection _dbConnection;
// Ctor
static ProductsDB()
{
string connectionString =
ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
_dbConnection = new SqlConnection(connectionString);
}
// Methods
public static DataSet SelectProducts()
{
string queryString = "SELECT [ProductID], [ProductName]FROM
[Products]";
IDbCommand dbCommand = new SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = _dbConnection;
IDbDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = dbCommand;
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
} // SelectProducts()
public static int UpdateProduct(int productID, string productName,
decimal unitPrice, short unitsInStock, bool discontinued)
{
string queryString = "UPDATE [Products] SET [ProductName] =
@ProductName WHERE [ProductID] = @ProductID";
IDbCommand dbUpdateCommand = new SqlCommand();
dbUpdateCommand.CommandText = queryString;
dbUpdateCommand.Connection = _dbConnection;
IDataParameter dbParam = new SqlParameter();
dbParam.ParameterName = "@ProductID";
dbParam.Value = productID;
dbParam.DbType = DbType.Int32;
dbUpdateCommand.Parameters.Add(dbParam);
dbParam = new SqlParameter();
dbParam.ParameterName = "@ProductName";
dbParam.Value = productName;
dbParam.DbType = DbType.String;
dbCommand.Parameters.Add(dbParam);
int rowsAffected = 0;
_dbConnection.Open();
try
{
rowsAffected = dbUpdateCommand.ExecuteNonQuery();
}
finally
{
_dbConnection.Close();
}
return rowsAffected;
} // UpdateProduct()
public static int DeleteProduct(int productID) // ERROR:
'productID' is always 0 ???
{
string queryString = "DELETE FROM [Products] WHERE [ProductID] =
@ProductID";
IDbCommand dbDeleteCommand = new SqlCommand();
dbDeleteCommand.CommandText = queryString;
dbDeleteCommand.Connection = _dbConnection;
IDataParameter dbParam = new SqlParameter();
dbParam.ParameterName = "@ProductID";
dbParam.Value = productID;
dbParam.DbType = DbType.Int32;
dbDeleteCommand.Parameters.Add(dbParam);
int rowsAffected = 0;
_dbConnection.Open();
try
{
rowsAffected = dbDeleteCommand.ExecuteNonQuery();
}
finally
{
_dbConnection.Close();
}
return rowsAffected;
} // DeleteProduct()
} // class ProductsDB
Thank you
Chris