Solved

Update GridView of ASP.net Project

Posted on 2013-05-14
11
226 Views
Last Modified: 2013-06-19
I am trying to add an update row to my gridview, unfortunately, I am unable to get that to work even though my select, delete and cancel work.  How would I do this?  Thanks.  Please see my code below:

<asp:SqlDataSource ID="sqldataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:FileLibraryConnection %>" 
    DeleteCommand="spDeleteDeploymentInfoById" DeleteCommandType="StoredProcedure" 
    InsertCommand="spInsertDeploymentInfo" InsertCommandType="StoredProcedure" 
    SelectCommand="spGetAllDeploymentInfo" SelectCommandType="StoredProcedure"
    UpdateCommand="UPDATE dbo.deploymentInfo
SET 
DeploymentSiteRoot = @DeploymentSiteRoot,
dbservername = dbservername,
MSAccessServiceAccount = @MSAccessServiceAccount,
MSExcelServiceAccount = @MSExcelServiceAccount,
MSPPointServiceAccount = @MSPPointServiceAccount,
MSVisioServiceAccount = @MSVisioServiceAccount,
MSWordServiceAccount = @MSWordServiceAccount,
MSRootWebServiceAccount = @MSRootWebServiceAccount,
TimeofCreation = @TimeofCreation,
UpdateTime = GETDATE()
WHERE
DeploymentId = @DeploymentId">    
    <DeleteParameters>
        <asp:Parameter Name="DeploymentId" Type="Int32" />
    </DeleteParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" 
    AutoGenerateEditButton="true"
    AutoGenerateColumns="False" 
    DataSourceID="sqldataSource1" 
    OnRowUpdating="GridView1_RowUpdating"
    BorderStyle="Solid" BorderColor="Black" DataKeyNames="DeploymentID">    
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
            ShowSelectButton="True" ShowInsertButton="True" />        
        <asp:BoundField DataField="DeploymentSiteRoot" HeaderText="DeploymentSiteRoot" 
            SortExpression="DeploymentSiteRoot" />
        <asp:BoundField DataField="dbservername" HeaderText="dbservername"
            SortExpression="dbservername" />
        <asp:BoundField DataField="MSAccessServiceAccount" 
            HeaderText="MSAccessServiceAccount" SortExpression="MSAccessServiceAccount" />
        <asp:BoundField DataField="MSExcelServiceAccount" 
            HeaderText="MSExcelServiceAccount" SortExpression="MSExcelServiceAccount" />
        <asp:BoundField DataField="MSPPointServiceAccount" 
            HeaderText="MSPPointServiceAccount" SortExpression="MSPPointServiceAccount" />
        <asp:BoundField DataField="MSVisioServiceAccount" 
            HeaderText="MSVisoServiceAccount" SortExpression="MSVisioServiceAccount" />
        <asp:BoundField DataField="MSWordServiceAccount" 
            HeaderText="MSWordServiceAccount" SortExpression="MSWordServiceAccount" />
        <asp:BoundField DataField="MSRootWebServiceAccount" 
            HeaderText="MSRootWebServiceAccount" SortExpression="MSRootWebServiceAccount" />
        <asp:BoundField DataField="TimeofCreation" HeaderText="TimeofCreation" 
            SortExpression="TimeofCreation" />
        <asp:BoundField DataField="UpdateTime" HeaderText="UpdateTime" SortExpression="UpdateTime" />
    </Columns>
</asp:GridView>

Open in new window

0
Comment
Question by:VBBRett
  • 7
  • 3
11 Comments
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 375 total points
ID: 39166655
Please post:
The exact error message/exception you have.
The CREATE table SQL of the target table.

I assume you tested that the update statement works OK in SQL server.

One way to detect error is to add this event to gridview: (OnRowUpdated=”grdv_RowUpdated”)
protected void grdv_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
if (e.Exception != null)
{
lblError.Text = e.Exception.Message;
e.ExceptionHandled = true;
}
}

Open in new window

0
 

Author Comment

by:VBBRett
ID: 39166767
No error message at all, it just doesn't update the grid row
0
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 375 total points
ID: 39166849
Does the update action update the database table DeploymentSiteRoot?
Does the grdv_RowUpdated event shows an y exception?
Has the sql user (connection string) has update permissions?
What version SQL server/VS are you using?

Another possible cause is that the select statement (being sp) does not define the sql parameters, if this is the case you need to specify
<UpdateParameters>                    
    <asp:Parameter Name="DeploymentId" Type="Int32" />
//and so on
</UpdateParameters>

Open in new window


Note: Please provide all info requested on previous post as well to avoid guessing work.
0
 

Author Comment

by:VBBRett
ID: 39167112
Here is my update command as can be seen in the above Sqldatasource:

UpdateCommand="UPDATE dbo.deploymentInfo
SET 
DeploymentSiteRoot = @DeploymentSiteRoot,
dbservername = dbservername,
MSAccessServiceAccount = @MSAccessServiceAccount,
MSExcelServiceAccount = @MSExcelServiceAccount,
MSPPointServiceAccount = @MSPPointServiceAccount,
MSVisioServiceAccount = @MSVisioServiceAccount,
MSWordServiceAccount = @MSWordServiceAccount,
MSRootWebServiceAccount = @MSRootWebServiceAccount,
TimeofCreation = @TimeofCreation,
UpdateTime = GETDATE()
WHERE
DeploymentId = @DeploymentId">

Open in new window


Yes, the user is an admin and has update permissions.  I am using SQL Server 2008 R2 and Visual Studio 2010
0
 

Author Comment

by:VBBRett
ID: 39167116
Here is the code that I do have...

<asp:SqlDataSource ID="sqldataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:FileLibraryConnection %>" 
    DeleteCommand="spDeleteDeploymentInfoById" DeleteCommandType="StoredProcedure" 
    InsertCommand="spInsertDeploymentInfo" InsertCommandType="StoredProcedure" 
    SelectCommand="spGetAllDeploymentInfo" SelectCommandType="StoredProcedure"
    UpdateCommand="UPDATE dbo.deploymentInfo
SET 
DeploymentSiteRoot = @DeploymentSiteRoot,
dbservername = dbservername,
MSAccessServiceAccount = @MSAccessServiceAccount,
MSExcelServiceAccount = @MSExcelServiceAccount,
MSPPointServiceAccount = @MSPPointServiceAccount,
MSVisioServiceAccount = @MSVisioServiceAccount,
MSWordServiceAccount = @MSWordServiceAccount,
MSRootWebServiceAccount = @MSRootWebServiceAccount,
TimeofCreation = @TimeofCreation,
UpdateTime = GETDATE()
WHERE
DeploymentId = @DeploymentId">  
    <UpdateParameters>
        <asp:Parameter Name="DeploymentId" Type="Int32" />
         <asp:Parameter Name="DeploymentSiteRoot" Type="String" />
        <asp:Parameter Name="dbservername" Type="String" />
        <asp:Parameter Name="MSAccessServiceAccount" Type="String" />
        <asp:Parameter Name="MSExcelServiceAccount" Type="String" />
        <asp:Parameter Name="MSPPointServiceAccount" Type="String" />
        <asp:Parameter Name="MSVisioServiceAccount" Type="String" />
        <asp:Parameter Name="MSWordServiceAccount" Type="String" />
        <asp:Parameter Name="MSRootWebServiceAccount" Type="String" />
        <asp:Parameter Name="UpdateTime" Type="DateTime" />
    </UpdateParameters>  
    <DeleteParameters>
        <asp:Parameter Name="DeploymentId" Type="Int32" />
    </DeleteParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" 
    AutoGenerateEditButton="true"
    AutoGenerateColumns="False" 
    DataSourceID="sqldataSource1"        
    BorderStyle="Solid" BorderColor="Black" DataKeyNames="DeploymentID">    
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
            ShowSelectButton="True" ShowInsertButton="True" />        
        <asp:BoundField DataField="DeploymentSiteRoot" HeaderText="DeploymentSiteRoot" 
            SortExpression="DeploymentSiteRoot" />
        <asp:BoundField DataField="dbservername" HeaderText="dbservername"
            SortExpression="dbservername" />
        <asp:BoundField DataField="MSAccessServiceAccount" 
            HeaderText="MSAccessServiceAccount" SortExpression="MSAccessServiceAccount" />
        <asp:BoundField DataField="MSExcelServiceAccount" 
            HeaderText="MSExcelServiceAccount" SortExpression="MSExcelServiceAccount" />
        <asp:BoundField DataField="MSPPointServiceAccount" 
            HeaderText="MSPPointServiceAccount" SortExpression="MSPPointServiceAccount" />
        <asp:BoundField DataField="MSVisioServiceAccount" 
            HeaderText="MSVisoServiceAccount" SortExpression="MSVisioServiceAccount" />
        <asp:BoundField DataField="MSWordServiceAccount" 
            HeaderText="MSWordServiceAccount" SortExpression="MSWordServiceAccount" />
        <asp:BoundField DataField="MSRootWebServiceAccount" 
            HeaderText="MSRootWebServiceAccount" SortExpression="MSRootWebServiceAccount" />
        <asp:BoundField DataField="TimeofCreation" HeaderText="TimeofCreation" 
            SortExpression="TimeofCreation" />
        <asp:BoundField DataField="UpdateTime" HeaderText="UpdateTime" SortExpression="UpdateTime" />
    </Columns>
</asp:GridView>

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 375 total points
ID: 39167303
Please post:
- The CREATE table SQL of the target table.
the answers to:
- Does the update action update the database table DeploymentSiteRoot? You can check that by inspecting the database and the SQL server logs - e.g profiler log. I need that to determine if the problem is in your page and what kind of error is logged.
- Does the grdv_RowUpdated event shows any exception at e.Exception?IF so please post it.
- Does the update statement runs OK in SQL server MS with the test values that are not updating in the gridview?
0
 
LVL 12

Assisted Solution

by:Jitendra Patil
Jitendra Patil earned 125 total points
ID: 39167641
hi VBBRett
i think you should check the below line in your code

<asp:SqlDataSource ID="sqldataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:FileLibraryConnection %>"
    DeleteCommand="spDeleteDeploymentInfoById" DeleteCommandType="StoredProcedure"
    InsertCommand="spInsertDeploymentInfo" InsertCommandType="StoredProcedure"
    SelectCommand="spGetAllDeploymentInfo" SelectCommandType="StoredProcedure"
    UpdateCommand="UPDATE dbo.deploymentInfo
SET
DeploymentSiteRoot = @DeploymentSiteRoot,
dbservername = dbservername,
MSAccessServiceAccount = @MSAccessServiceAccount,
MSExcelServiceAccount = @MSExcelServiceAccount,


the above line in bold shows that you are assign a variable value to the parmeter, so it should be:
dbservername = @dbservername,

as you have declared dbservername as variable in the list of update parameters
<UpdateParameters>
        <asp:Parameter Name="DeploymentId" Type="Int32" />
         <asp:Parameter Name="DeploymentSiteRoot" Type="String" />
<asp:Parameter Name="dbservername" Type="String" />
        <asp:Parameter Name="MSAccessServiceAccount" Type="String" />
        <asp:Parameter Name="MSExcelServiceAccount" Type="String" />
        <asp:Parameter Name="MSPPointServiceAccount" Type="String" />
        <asp:Parameter Name="MSVisioServiceAccount" Type="String" />
        <asp:Parameter Name="MSWordServiceAccount" Type="String" />
        <asp:Parameter Name="MSRootWebServiceAccount" Type="String" />
        <asp:Parameter Name="UpdateTime" Type="DateTime" />
    </UpdateParameters>  

try changing the code and also pass value to the dbservername variable.

hope this helps.
0
 

Author Comment

by:VBBRett
ID: 39168789
Negative, I'm still where I left off.  The update query works but for some reason, the parameters are not matching up the text box field names to the sql parameters.  I ran an example and as opposed to having @dbservername, I typed in 'test'.  'test' was inserted into that field and the data was updated just fine.  So, how do I get it so that my update query reads the gridview textboxes like it is supposed to?
0
 

Accepted Solution

by:
VBBRett earned 0 total points
ID: 39248223
Ok, so it appears that once I took the DeploymentId out as an Update Parameter on the <UpdateParamaters> section, I was good to go.  Since the DeploymentId is a Primary Key and is stated as such in the grid, I did not need to add the DeploymentId as a paramater in the Update Parameters section.
0
 

Author Comment

by:VBBRett
ID: 39248230
I'll still give points or effort, but the answer is what I stated above.
0
 

Author Closing Comment

by:VBBRett
ID: 39258781
My answer was the overall correct answer.  All other explanations were good explanations and keypoints for other users, but I was the only one to find the correct resolution.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now