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 5 2154
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Bailey |
last post by:
Is there a good way to databind a textbox to a date field that will not throw
an error when a null is encountered. I tried changing the Null Value in the
dataset , but when I do I get the...
|
by: Trail Monster |
last post by:
Ok, I've been searching the net now for several days and can't find how to do
this anywhere.
Version:
VS 2005 Professional Release, 2.0 Framework
Background:
I have a complex business object...
|
by: Eiriken |
last post by:
Hello everyone,
I am using ASP.NET 2 and trying to bind a objectdatasource to a
gridview. By doing this the most common way by adding an
objectdatasource to the page and by using the wizard to...
|
by: Northern |
last post by:
Hello,
I have some trouble to declare and instantiate dynamically an
ObjectDataSource in the codebehind file. The idea is to bind the
objectdatasource to a gridview and have automate sorting and...
|
by: marianowic |
last post by:
Hello.
I`m killing my computer for 2 days and I can`y figure it out. First I
will present the situation:
I`m building and WWW system in VS 2005. I`m using a MaterPage ( maybe
this is the problem...
|
by: tarscher |
last post by:
Hi all,
I have a DataSource linked to a gridview and have delete functionality
on the gridview
The objectdatasource looks like this:
<asp:ObjectDataSource ID="TestsObjectDataSource"...
|
by: Roger |
last post by:
Hi all
On a page I am using a repeater which is linked to an ObjectDataSource
(same page):
<asp:Repeater ID="VideoListNewestRepeater" runat="server"
DataSourceID="VideoListNewestDataSource">...
|
by: Mike |
last post by:
Hi all
I'm trying to do something that seem rather simple in theory but just
doesn't want to work out.
I have a form with a text box used to type in a search criteria, a drop
down list used...
|
by: TheDude5B |
last post by:
HI,
I am creating a DataList by using the SelectMethod from an
ObjectDataSource. Within each item I want to be able to the
dynamically create a drop down list depending on what the ProductID is...
|
by: btreddy |
last post by:
Hii all,
I've a problem regarding passing select parameters to objectdatasource from code behind.
i would like to pass the select parameters to a objectdatasource ..not in the selecting or...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| | |