PC Review


Reply
Thread Tools Rate Thread

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

 
 
cmrchs@gmail.com
Guest
Posts: n/a
 
      30th Jul 2008
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
 
Reply With Quote
 
 
 
 
Yankee Imperialist Dog
Guest
Posts: n/a
 
      30th Jul 2008
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!


"(E-Mail Removed)" 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
>

 
Reply With Quote
 
Christian Cambier
Guest
Posts: n/a
 
      30th Jul 2008
On Jul 30, 6:55 pm, Yankee Imperialist Dog
<YankeeImperialist...@discussions.microsoft.com> wrote:
> 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
 
Reply With Quote
 
cmrchs@gmail.com
Guest
Posts: n/a
 
      31st Jul 2008
On Jul 30, 6:55 pm, Yankee Imperialist Dog
<YankeeImperialist...@discussions.microsoft.com> wrote:
> 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
 
Reply With Quote
 
Yankee Imperialist Dog
Guest
Posts: n/a
 
      31st Jul 2008
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!


"(E-Mail Removed)" wrote:

> On Jul 30, 6:55 pm, Yankee Imperialist Dog
> <YankeeImperialist...@discussions.microsoft.com> wrote:
> > 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
>

 
Reply With Quote
 
Yankee Imperialist Dog
Guest
Posts: n/a
 
      31st Jul 2008
continued
or for update ....
--
Share The Knowledge. I need all the help I can get and so do you!


"(E-Mail Removed)" wrote:

> On Jul 30, 6:55 pm, Yankee Imperialist Dog
> <YankeeImperialist...@discussions.microsoft.com> wrote:
> > 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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Databinding to a DAL with ObjectDataSource-control: Update OK, Deletenot (debug info provided) please help! cmrchs@gmail.com Microsoft ASP .NET 0 31st Jul 2008 07:37 PM
Prevent ObjectDataSource/GridView from DataBinding richardl@conceptsearching.com Microsoft ASP .NET 1 18th Apr 2007 07:08 PM
ObjectDataSource with Complex Objects and 2 way Databinding =?Utf-8?B?VHJhaWwgTW9uc3Rlcg==?= Microsoft ASP .NET 5 12th Jan 2007 10:53 PM
Databinding problems with ObjectDataSource Richard Carpenter Microsoft ASP .NET 0 18th Jul 2006 06:09 PM
Asp.net databinding to objectdatasource =?Utf-8?B?UmFt?= Microsoft ASP .NET 2 1st Dec 2005 05:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:06 PM.