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

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>
<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
 
Y

Yankee Imperialist Dog

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!


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
 
C

Christian Cambier

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!

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
 
C

cmrchs

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!

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
 
Y

Yankee Imperialist Dog

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!


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!

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
 
Y

Yankee Imperialist Dog

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


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!

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top