我是asp.net的新手,所以GridView和我很难完成以下任务: T在我的数据库中有三个表:
Bookings
--------------------------------------------------------
id | start_date | end_date | user_id | pet_id
----|---------------|--------------|-----------|--------
1 | 01/04/2011 | 01/04/2011 | 1 | 2
--------------------------------------------------------
Pets
---------------------------------------------------------
id | name | species | owner_id | caretaker_id
-----|-------------|----------|------------|-------------
1 | samplename | dog | 1 | 2
---------------------------------------------------------
Users
------------------------------------------------------------------------
id | email | first_name | last_name | password
------|----------------------|-------------|--------------|-------------
1 | test@test.gmail.com | Name | Surname | passwordhere
------------------------------------------------------------------------
我想要做的是使用数据网格显示预订表中的信息,使其可编辑,以便用户可以编辑他的宠物的预订。使用自动sql语句的初始尝试工作正常,SELECT * from [bookings]
返回了预订表,我能够删除和更新行。
我决定使用新的选择查询,以便用户清楚地看到他的宠物的名字和他的名字作为所有者而不是数字。我使用了以下选择查询,它非常有效:
select start_date, end_date, usr.first_name AS 'Owner', pets.name AS 'Pet name' from
bookings AS bookings left join users AS usr ON bookings.user_id=usr.id left join
pets AS pets ON pets.id=bookings.pet_id
Output
----------------------------------------------------------
start_date | end_date | Owner | Pet name |
---------------|-------------|-------------|-------------|
01/04/2011 | 01/04/2011 | Name | samplename |
----------------------------------------------------------
现在问题出现了:从我将GridView中的select查询更新为上述问题时,没有删除和更新语句。当然我会提供自定义的,但无论我尝试什么,它总是返回错误incorrect syntax near nvarchar
。
我也不确定pet_id列是整数还是字符串,因为它在新的连接选择中显示pet的名称而不是它的id?
我不知道我在这里做错了什么。我感谢任何帮助。下面GridView的代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="start_date" HeaderText="start_date" SortExpression="start_date" />
<asp:BoundField DataField="end_date" HeaderText="end_date" SortExpression="end_date" />
<asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" />
<asp:BoundField DataField="Pet name" HeaderText="Pet name" SortExpression="Pet name" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pph_database %>" SelectCommand="select start_date, end_date, usr.first_name AS 'Owner', pets.name AS 'Pet name' from bookings AS bookings left join users AS usr ON bookings.user_id=usr.id left join pets AS pets ON pets.id=bookings.pet_id" UpdateCommand="UPDATE [bookings] SET [start_date] = @start_date, [end_date] = @end_date, [user_id] = @user_id, [pet_id] = @pet_id WHERE [id] = @id" DeleteCommand="DELETE FROM [bookings] WHERE [id] = @id" InsertCommand="INSERT INTO [bookings] ([start_date], [end_date], [user_id], [pet_id]) VALUES (@start_date, @end_date, @user_id, @pet_id)">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter DbType="Date" Name="start_date" />
<asp:Parameter DbType="Date" Name="end_date" />
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="pet_id" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter DbType="Date" Name="start_date" />
<asp:Parameter DbType="Date" Name="end_date" />
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="pet_id" Type="Int32" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
答案 0 :(得分:1)
也许你会这样做:
<asp:GridView ID="GridView1" runat="server" DataMember="Table"
EnableModelValidation="True" CssClass = "GridViewStyle"
OnRowDeleting = "dataViewRowDeleting" AutoGenerateDeleteButton="True"
OnRowEditing = "dataViewRowEditing" AutoGenerateEditButton="True"
OnRowUpdating = "dataViewRowUpdating">
<Columns>
...
</Columns>
</asp:GridView>
您可以将事件dataViewRowDeleting与行删除相关联,将dataViewRowEditing与行编辑相关联。