Reputation: 187
This is probably something simple, just need some fresh eyes. It's very long, but I wanted to give full context. I'm trying to show a GridView that uses an ObjectDataSource. The ODS returns a DataTable. The DT is constructed from the query result of an MSSQL stored procedure. The problem is that my ASP BoundField is complaining at me for wanting a field that isn't in the data source...but it definitely is. When I leave out the aforementioned field, the grid shows fine, but when I press "Delete", an id of 0 is sent to my ODS, not the one that actually deletes the records, 70. I haven't tried updating yet, but I imagine that doesn't work if the other two don't. What am I doing wrong?
GridView:
<asp:GridView
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False"
CellPadding="4"
CssClass="table"
DataSourceID="ExcludeODS"
EnableViewState="True"
ForeColor="#333333"
GridLines="None"
ID="ExcludeGridView"
runat="server">
<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="RuleKey" HeaderText="Rule Key"></asp:BoundField>
<asp:BoundField DataField="Field" HeaderText="Field"></asp:BoundField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton
runat="server"
CommandName="Edit"><span class="fa fa-pencil"> Edit</span>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton
runat="server"
CommandName="Delete"
OnClientClick="if(!confirm('Are you sure you want to delete this?')){ return false; };"><span class="fa fa-trash"> Delete</span>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF"></EditRowStyle>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"></FooterStyle>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" BackColor="#2461BF" ForeColor="White"></PagerStyle>
<RowStyle BackColor="#EFF3FB"></RowStyle>
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"></SelectedRowStyle>
<SortedAscendingCellStyle BackColor="#F5F7FB"></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle BackColor="#6D95E1"></SortedAscendingHeaderStyle>
<SortedDescendingCellStyle BackColor="#E9EBEF"></SortedDescendingCellStyle>
<SortedDescendingHeaderStyle BackColor="#4870BE"></SortedDescendingHeaderStyle>
</asp:GridView>
ObjectDataSource (.ASPX):
<asp:ObjectDataSource
runat="server"
ID="ExcludeODS"
DeleteMethod="CtDeleteExcludeRules"
InsertMethod="CtAddExcludeRules"
SelectMethod="CtGetExcludeRules"
TypeName="CustomerTypeRules.DAL.CtExcludeRules"
UpdateMethod="CtUpdateExcludeRules">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" ></asp:Parameter>
<asp:Parameter Name="userId" Type="String"></asp:Parameter>
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="ruleKey" Type="String"></asp:Parameter>
<asp:Parameter Name="field" Type="String"></asp:Parameter>
<asp:Parameter Name="userId" Type="String"></asp:Parameter>
</InsertParameters>
<SelectParameters>
<asp:ControlParameter
ControlID="RuleDropDown"
PropertyName="SelectedValue"
DefaultValue="null"
Name="ruleKey"
Type="String">
</asp:ControlParameter>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32"></asp:Parameter>
<asp:Parameter Name="field" Type="String"></asp:Parameter>
<asp:Parameter Name="userId" Type="String"></asp:Parameter>
</UpdateParameters>
</asp:ObjectDataSource>
ObjectDataSource (.CS):
public DataTable CtGetExcludeRules(
string ruleKey // Length: 255
)
{
SqlConnection dbConn = new SqlConnection(_connectString);
SqlCommand command =
new SqlCommand
{
CommandText = "CT_Get_ExcludeRules",
CommandType = CommandType.StoredProcedure,
Connection = dbConn
};
command.Parameters.Add(
"@RuleKey", SqlDbType.VarChar, 255
).Value = ruleKey ?? "";
dbConn.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
dbConn.Close();
da.Dispose();
return dt.Rows.Count > 0 ? dt : new DataTable{Columns = { "ruleKey", "field" }, Rows = { "No results" }};
}
public bool CtDeleteExcludeRules(
int id,
string userId // Length: 50
)
{
using (var dbConn = new SqlConnection(_connectString))
{
using (var command = new SqlCommand(
"CT_Delete_ExcludeRules", dbConn
))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(
"@Id", SqlDbType.Int
).Value = id;
command.Parameters.Add(
"@UserId", SqlDbType.VarChar, 50
).Value = userId ?? "";
dbConn.Open();
return command.ExecuteNonQuery() > 0;
}
}
}
public bool CtUpdateExcludeRules(
int id,
string field, // Length: 50
string userId // Length: 50
)
{
using (var dbConn = new SqlConnection(_connectString))
{
using (var command = new SqlCommand(
"CT_Update_ExcludeRules", dbConn
))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(
"@Id", SqlDbType.Int
).Value = id;
command.Parameters.Add(
"@Field", SqlDbType.VarChar, 50
).Value = field;
command.Parameters.Add(
"@UserId", SqlDbType.VarChar, 50
).Value = System.Web.HttpContext.Current.User.Identity.Name
?? "";
dbConn.Open();
return command.ExecuteNonQuery() > 0;
}
}
}
Stored procedures:
[dbo].[CT_Get_ExcludeRules]
@RuleKey varchar(255) = ''
AS
SELECT * FROM [Interface].[dbo].[CT_ExcludeRules] WHERE RuleKey = @RuleKey;
[dbo].[CT_Delete_ExcludeRules]
@Id int,
@UserId varchar(50)
as
BEGIN TRANSACTION;
BEGIN TRY
insert into CT_ExcludeRules_History
select Id,RuleKey,Field,@UserId,'Delete',getdate()
from CT_ExcludeRules
where Id = @Id
delete from [dbo].[CT_ExcludeRules]
where Id = @Id
Commit
END TRY
[CT_Update_ExcludeRules]
@Id int,
@Field varchar(50),
@UserId varchar(50)
as
BEGIN TRANSACTION;
BEGIN TRY
Update [dbo].[CT_ExcludeRules]
set Field = @Field
where Id = @Id
insert into CT_ExcludeRules_History
select Id,RuleKey,Field,@UserId,'Update',getdate()
from CT_ExcludeRules
where Id = @Id
Commit
END TRY
Table design:
GET stored procedure example:
DataTable Rows Results View (also the same array that is retrieved by the data source's OnSelected event):
Values passed to data source after clicking "Delete":
Trying to add an Id column to the GridView gives me this error message:
A field or property with the name 'Id' was not found on the selected data source.
I don't need an id column, I just want to delete and update like a normal GridView!
RESOLUTION:
Thanks to user "sea-charp" below for seeing what was missing. I needed to replace { "ruleKey", "field" } with { "id", "ruleKey", "field" } in order for my data source to recognize the "Id" property. Once that was recognized, I was able to add "Id" in the DataKeyNames attribute of my GridView, which fully enabled my deletion method via the proper Id of 70 instead of 0.
Upvotes: 0
Views: 315
Reputation: 298
Instead of this :
command.Parameters.Add(
"@RuleKey", SqlDbType.VarChar, 255
).Value = ruleKey ?? "";
Try this
command.Parameters.Add("@RuleKey", SqlDbType.VarChar, 255);
command.Parameters("@RuleKey").Value = ruleKey ?? "";
Looks like you're getting the error because you have this as part of your return syntax:
DataTable{Columns = { "ruleKey", "field" }, Rows = { "No results" }};
... which returns a table with no ID when there are no results - possibly because of the command parameter syntax you're using.
Upvotes: 1