By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,376 Members | 1,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,376 IT Pros & Developers. It's quick & easy.

Databinding to a DAL with ObjectDataSource-control: Update OK, DeleteNot OK ???

P: n/a
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>
<asp:Parameter Name="productID" Type="Int32" />
<asp:Parameter Name="productName" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter 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
Jul 30 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Does delete fire rowcommand? and can you see what the value is there. Could
be the row is reall not firing where you think

I use custom business object rather than datsets but the principle is the
same. I switched because tableadapters and generated objects left me feel
less in control.

for future you may want to check out this artical:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416.

i use it and have a code generator (webbased from table, sp's to all objects
+ a CRUD page.)
--
Share The Knowledge. I need all the help I can get and so do you!
"cm****@gmail.com" wrote:
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>
<asp:Parameter Name="productID" Type="Int32" />
<asp:Parameter Name="productName" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter 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
Jul 30 '08 #2

P: n/a
On Jul 30, 6:55 pm, Yankee Imperialist Dog
<YankeeImperialist...@discussions.microsoft.comwro te:
Does delete fire rowcommand? and can you see what the value is there. Could
be the row is reall not firing where you think

I use custom business object rather than datsets but the principle is the
same. I switched because tableadapters and generated objects left me feel
less in control.

for future you may want to check out this artical:http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416.

i use it and have a code generator (webbased from table, sp's to all objects
+ a CRUD page.)

--
Share The Knowledge. I need all the help I can get and so do you!

"cmr...@gmail.com" wrote:
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>
<asp:Parameter Name="productID" Type="Int32" />
<asp:Parameter Name="productName" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter 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
hello,

Rowcommand gets fired yes.

I implemented

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
string s = string.Format("{0} / {1} / {2}", e.CommandArgument,
e.CommandName, e.CommandSource);
Label1.Text = s;
}

Label1.Text says for example:

3 / Delete / System.Web.UI.WebControls.GridView

3 is the forth record but is not the ProductID.

Now what?

It's just that I have to make a demo work, and the demo is using the
GridView and ObjectDataSource.

thank you
Chris
Jul 30 '08 #3

P: n/a
On Jul 30, 6:55 pm, Yankee Imperialist Dog
<YankeeImperialist...@discussions.microsoft.comwro te:
Does delete fire rowcommand? and can you see what the value is there. Could
be the row is reall not firing where you think

I use custom business object rather than datsets but the principle is the
same. I switched because tableadapters and generated objects left me feel
less in control.

for future you may want to check out this artical:http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416.

i use it and have a code generator (webbased from table, sp's to all objects
+ a CRUD page.)

--
Share The Knowledge. I need all the help I can get and so do you!

"cmr...@gmail.com" wrote:
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>
<asp:Parameter Name="productID" Type="Int32" />
<asp:Parameter Name="productName" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter 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
hello,

Rowcommand gets fired yes.

I implemented

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
string s = string.Format("{0} / {1} / {2}", e.CommandArgument,
e.CommandName, e.CommandSource);
Label1.Text = s;
}

Label1.Text says for example:

3 / Delete / System.Web.UI.WebControls.GridView

3 is the forth record but is not the ProductID.

Now what?

It's just that I have to make a demo work, and the demo is using the
GridView and ObjectDataSource.

thank you
Chris
Jul 30 '08 #4

P: n/a
just a quick check
is the Product ID in the keys "bag" for the GridView.
find it by the row index gridview.key
int i = Convert.ToInt32(gvCatalog.DataKeys[Index].Values["keyname"]);

depending on how it's bound you may need to call the

delete on the datalayer in the row event

myProduct.Delete(i);
--
Share The Knowledge. I need all the help I can get and so do you!
"cm****@gmail.com" wrote:
On Jul 30, 6:55 pm, Yankee Imperialist Dog
<YankeeImperialist...@discussions.microsoft.comwro te:
Does delete fire rowcommand? and can you see what the value is there. Could
be the row is reall not firing where you think

I use custom business object rather than datsets but the principle is the
same. I switched because tableadapters and generated objects left me feel
less in control.

for future you may want to check out this artical:http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416.

i use it and have a code generator (webbased from table, sp's to all objects
+ a CRUD page.)

--
Share The Knowledge. I need all the help I can get and so do you!

"cmr...@gmail.com" wrote:
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>
<asp:Parameter Name="productID" Type="Int32" />
<asp:Parameter Name="productName" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter 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

hello,

Rowcommand gets fired yes.

I implemented

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
string s = string.Format("{0} / {1} / {2}", e.CommandArgument,
e.CommandName, e.CommandSource);
Label1.Text = s;
}

Label1.Text says for example:

3 / Delete / System.Web.UI.WebControls.GridView

3 is the forth record but is not the ProductID.

Now what?

It's just that I have to make a demo work, and the demo is using the
GridView and ObjectDataSource.

thank you
Chris
Jul 31 '08 #5

P: n/a
continued
or for update ....
--
Share The Knowledge. I need all the help I can get and so do you!
"cm****@gmail.com" wrote:
On Jul 30, 6:55 pm, Yankee Imperialist Dog
<YankeeImperialist...@discussions.microsoft.comwro te:
Does delete fire rowcommand? and can you see what the value is there. Could
be the row is reall not firing where you think

I use custom business object rather than datsets but the principle is the
same. I switched because tableadapters and generated objects left me feel
less in control.

for future you may want to check out this artical:http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416.

i use it and have a code generator (webbased from table, sp's to all objects
+ a CRUD page.)

--
Share The Knowledge. I need all the help I can get and so do you!

"cmr...@gmail.com" wrote:
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>
<asp:Parameter Name="productID" Type="Int32" />
<asp:Parameter Name="productName" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter 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

hello,

Rowcommand gets fired yes.

I implemented

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
string s = string.Format("{0} / {1} / {2}", e.CommandArgument,
e.CommandName, e.CommandSource);
Label1.Text = s;
}

Label1.Text says for example:

3 / Delete / System.Web.UI.WebControls.GridView

3 is the forth record but is not the ProductID.

Now what?

It's just that I have to make a demo work, and the demo is using the
GridView and ObjectDataSource.

thank you
Chris
Jul 31 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.