Solved

Update GridView of ASP.net Project

Posted on 2013-05-14
11
227 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

11 Experts available now in Live!

Get 1:1 Help Now