Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

Update in asp.net gridview when binding with SqlDataSource

I have a GridView that is bind with SqlDataSource1 in .aspx page.But when I use event "OnRowUpdating" and update it with code behind .An exception occur that

Updating is not supported by data source 'SqlDataSource1' unless UpdateCommand is specified.

Gridview that I am using is

   <asp:GridView ID="gdvProfiles" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SqlDataSource1"
            OnRowUpdating="updateRecord" OnRowEditing="editRecord" OnRowCancelingEdit="cancelRecord"
            AutoGenerateColumns="False" CellPadding="0" ForeColor="#333333" GridLines="None"
            Style="margin-right: 38px" Font-Size="Small">

and OnRowUpdaing

   protected void updateRecord(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            Label lblusername = gdvProfiles.Rows[e.RowIndex].FindControl("lblusername") as Label;
            TextBox txtname = gdvProfiles.Rows[e.RowIndex].FindControl("txtname") as TextBox;
            TextBox txtsponser = gdvProfiles.Rows[e.RowIndex].FindControl("txtsponser") as TextBox;
            TextBox txtemail = gdvProfiles.Rows[e.RowIndex].FindControl("txtemail") as TextBox;
            TextBox txtdob = gdvProfiles.Rows[e.RowIndex].FindControl("txtdob") as TextBox;

            TextBox txthomecontact = gdvProfiles.Rows[e.RowIndex].FindControl("txthomecontact") as TextBox;
            TextBox txtcontact = gdvProfiles.Rows[e.RowIndex].FindControl("txtcontact") as TextBox;
            TextBox txtaddress = gdvProfiles.Rows[e.RowIndex].FindControl("txtaddress") as TextBox;
            TextBox txtcity = gdvProfiles.Rows[e.RowIndex].FindControl("txtcity") as TextBox;
            TextBox txtstate = gdvProfiles.Rows[e.RowIndex].FindControl("txtstate") as TextBox;
            TextBox txtzipcode = gdvProfiles.Rows[e.RowIndex].FindControl("txtzipcode") as TextBox;
            TextBox txtcountry = gdvProfiles.Rows[e.RowIndex].FindControl("txtcountry") as TextBox;




            BORegistration oBORegistration = new BORegistration();
            BalRegistration oBalRegistration = new BalRegistration();
            oBORegistration.Name = lblusername.Text;

            oBORegistration.Name = txtname.Text;
            oBORegistration.Sponser = txtsponser.Text;
            oBORegistration.Email = txtemail.Text;
            oBORegistration.Dob = txtdob.Text;

            oBORegistration.HomeContact = txthomecontact.Text;
            oBORegistration.Contact = txtcontact.Text;
            oBORegistration.Addressx = txtaddress.Text;
            oBORegistration.City = txtcity.Text;
            oBORegistration.State = txtstate.Text;
            oBORegistration.ZipCode = txtzipcode.Text;
            oBORegistration.Country = txtcountry.Text;



            if (oBalRegistration.Updatememberprofile(oBORegistration))
            {
                gdvProfiles.EditIndex = -1;
                // FillData();
                gdvProfiles.DataBind();
            }
        }
        catch
        {
            throw;
        }
    }

the SqlDataSounce is

  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Universal %>"
        ProviderName="System.Data.SqlClient" SelectCommand="spGetMemberProfile" SelectCommandType="StoredProcedure"
        FilterExpression="Username LIKE '{0}%' OR Name LIKE '{0}%' OR           
        Sponser LIKE '{0}%' OR Email LIKE '{0}%' OR Dob LIKE '{0}%' OR
        HomeContact LIKE '{0}%' OR Contact LIKE '{0}%' OR City LIKE '{0}%' OR
        Statex LIKE '{0}%' OR ZipCode LIKE '{0}%' OR Country LIKE '{0}%'
        ">
        <FilterParameters>
            <asp:ControlParameter Name="ID" ControlID="txtFilter" />
        </FilterParameters>
    </asp:SqlDataSource>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have done this by mention UpdateCommand in SqlDataSqouce as

   <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Universal %>"
        ProviderName="System.Data.SqlClient" SelectCommand="spGetMemberProfile" UpdateCommand="spGetMemberProfile"
        SelectCommandType="StoredProcedure" FilterExpression="Username LIKE '{0}%' OR Name LIKE '{0}%' OR           
        Sponser LIKE '{0}%' OR Email LIKE '{0}%' OR Dob LIKE '{0}%' OR
        HomeContact LIKE '{0}%' OR Contact LIKE '{0}%' OR City LIKE '{0}%' OR
        Statex LIKE '{0}%' OR ZipCode LIKE '{0}%' OR Country LIKE '{0}%'
        ">
        <FilterParameters>
            <asp:ControlParameter Name="ID" ControlID="txtFilter" />
        </FilterParameters>
    </asp:SqlDataSource>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...