简体   繁体   中英

Add and save data in SQL Server

This is my table in SQL Server:

Create table SubTypes
(
    IDSub int primary key identity(1,1),
    Offerstype varchar(255) not null,
    CostOffers varchar(25) not null
);

Code in asp.net

<fieldset>
    <legend>ALL Offers</legend>
    <asp:Repeater ID="Offers" runat="server" >
        <HeaderTemplate>
            <table class="table table-hover">
                <tr>
                    <th>ID</th>
                    <th>Offers type</th>
                    <th>Cost</th>
                    <th>Delete</th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><%#Eval("IDSub")%></td>
                <td><%#Eval("Offerstype")%></td><--error-->
                <td><%#Eval("CostOffers")%></td>
                <td><%#Eval("delete from SubTypes where IDSub=@IDSub;")%></td>
            </tr>
            </table>
        </ItemTemplate>
    </asp:Repeater>
</fieldset>

C#

SportsCenterEntities db = new SportsCenterEntities();

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) {
        Offers_DataBinding();
    }
}

public void Offers_DataBinding()
{
    Offers.DataSource = db.SubTypes.ToList();
    Offers.DataBind();
}

Error

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: DataBinding: 'System.Data.Entity.DynamicProxies.SubType_084D6F49655DAD7C286182B0A82194429DD9DDEC3EA06D3CB3ED078CA68E2371' does not contain a property with the name 'SubType'.

图片

You can do this:

<table class="table table-hover">
<asp:Repeater ID="Offers" runat="server" OnItemCommand="Delete"> >
    <HeaderTemplate>            
            <tr>
                <th>ID</th>
                <th>Offers type</th>
                <th>Cost</th>
                <th>Delete</th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><%#Eval("IDSub")%></td>
            <td><%#Eval("Offerstype")%></td><--error-->
            <td><%#Eval("CostOffers")%></td>
            <td><asp:Button ID="btnDelete" runat="server" IDSub='<%#Eval("IDSub")%>' Text="Delete"/></td>
        </tr>            
    </ItemTemplate>
</asp:Repeater>
</table>

And in code behind catch the event sent from row:

protected void Delete(Object Sender, RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Button clickedDeleteButton= (Button)e.Item.FindControl("btnDelete"); 
            int idSub = int.Parse(clickedDeleteButton.Attributes["IDSub"]);
            // Here Delete codes by id...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM