C# Visual Studio 2012 - Devexpress gridView - saving data back to database

Posted on 2013-10-01
Medium Priority
Last Modified: 2013-10-17
Hi experts,

I have a table pulled from SQL where I am allowing the user to edit some of the fields and be able to hit a save button to update it in sql. My challenge is that I need to give the user the ability to hit save at any time as they're editing a field and be able to save back to the db. They do not need to get out of the row or the cell. They could type something half way in a cell and still be able to save it. What's the best way to do this?

Question by:timberadmin

Assisted Solution

XGIS earned 1000 total points
ID: 39539200
I actually use a hidden button in a panel to achieve the same ballpark result.  When the user leaves the textbox the button automatically clicks and saves to the DB.

this sample is without a gridview that requires modified code..  it filters the id in the same way and avoids painful grid c# syntax etc

       <asp:Panel ID="Panel1" runat="server" Style="visibility: hidden;" Height="1px">
            <asp:Button ID="btnUploadImageUpdateTitle" runat="server" Style="visibility: hidden;" OnClick="btnUploadImageUpdateTitle_Click" Width="1" ForeColor="#003756" BackColor="#003756" BorderColor="#003756" Height="1" />

Open in new window

It then runs the following code; this updates 3 fields in sql. It should help get you going at least.. Just make the button visible if you want them to click it. Otherwise it just saves automatically..One less button for people to click

protected void btnUploadImageUpdateTitle_Click(object sender, EventArgs e)
        string siteid = SiteID.ToString();  //I usually use a session variable to get the filter here

                    string Usql = "";

                    Usql = "UPDATE dbo.YourTableName SET Image10 = @Image10, Image10OriginalName = @Image10OriginalName, Image10Title = @Image10Title";
                    Usql += " WHERE  SiteID=" + SiteID.ToString();

                    SqlCommand Ucom = new SqlCommand();
                    if (Session["Image10"].ToString() != "0")
                        Ucom.Parameters.Add("@Image10", SqlDbType.VarChar).Value = DMPDBSession.Image10.ToString();
                        Ucom.Parameters.Add("@Image10OriginalName", SqlDbType.VarChar).Value = DMPDBSession.Image10OriginalName.ToString();
                        Ucom.Parameters.Add("@Image10Title", SqlDbType.VarChar).Value = tbxTitleImg10.Text.ToString();
                        Ucom.Parameters.Add("@Image10", SqlDbType.VarChar).Value = "";
                        Ucom.Parameters.Add("@Image10OriginalName", SqlDbType.VarChar).Value = "";
                        Ucom.Parameters.Add("@Image10Title", SqlDbType.VarChar).Value = "";
                        data.ExecuteNonQuery(Usql, Ucom);
                    catch (Exception ex)
                        Response.Write("<script>alert('" + Server.HtmlEncode(ex.ToString()) + "')</script>");

Open in new window

this is .net 4.5....try changing your field names and types where required.  You will obviously need to match the fields with your Markup etc.

i think the code below was actually my autosubmit code.  I had the same thing running slightly different on the same page. It was originally from a drag and drop image and auto update database type page.

 <asp:Button ID="btnAutoSubmitImage1" runat="server" Style="visibility: hidden;"
                                Visible="False" />
                                    function submitFile(sender, e) {
                                        $get("<%= btnAutoSubmitImage1.ClientID %>").click();
                                    function submitFile(sender, e) {
                                        $get("<%= btnUploadImageUpdateTitle.ClientID %>").click();


Open in new window

LVL 13

Accepted Solution

jonnidip earned 1000 total points
ID: 39539309
I believe that the field you are editing is committed (in the data context) at the time you focus away from it.
When you hit your "Save" button you are un-focusing the field being edited.
I think it is sufficient to get your context and persist it to the db.

Furthermore, I suggest you to contact the DevExpress support, as they seem to be very responsive on any kind problem or question.


Author Closing Comment

ID: 39580472
Found the exact answer on the DevExpress forums. Thanks guys.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question