|
-
Aug 6th, 2009, 04:46 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Gridview update from stored procedure
How do you programatically update a row in a gridview using a stored procedure.
Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each row As GridViewRow In Me.assignmentGridView.Rows
Me.assignmentDataSource.Update()
'where the update uses the parameters are defined below in .aspx file
Next
EndSub
the above code does not do anything, but its the idea of what Im trying to do.
Code:
<asp:SqlDataSource ID="assignmentDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:FCRscheduleConnectionString %>"
SelectCommand="dayAssignment" SelectCommandType="StoredProcedure" UpdateCommand="dayAssignmentUpdate" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="Calendar1" Name="date" PropertyName="SelectedDate"
Type="DateTime" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
<asp:ControlParameter ControlID="Calendar1" Name="date" PropertyName="SelectedDate"
Type="DateTime" />
<asp:ControlParameter ControlID="assignmentGridView" Name="fcrID" PropertyName="SelectedValue"
Type="Int32" />
<asp:ControlParameter ControlID="assignmentGridView" Name="Site" PropertyName="SelectedValue"
Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Last edited by Samir Nagheenanajar; Aug 6th, 2009 at 04:49 PM.
Reason: make more clear
censored
-
Aug 7th, 2009, 01:43 AM
#2
Re: Gridview update from stored procedure
Step through your code - in the button1.click method, do a quickwatch on assignmentDataSource.UpdateParameters. Are these parameters available to you? Do they have values?
-
Aug 7th, 2009, 02:43 AM
#3
Hyperactive Member
Re: Gridview update from stored procedure
-
Aug 7th, 2009, 09:10 AM
#4
Thread Starter
Addicted Member
Re: Gridview update from stored procedure
I think i might be on the right track. There are the correct number of assignmentDataSource.UpdateParameters, but im not sure how to view thier values. I tried just calling Me.assignmentGridView.UpdateRow(i, False) where i indexes the rows in the gridview. But this gives me an error
"Procedure or function dayAssignmentUpdate has too many arguments specified. "
assignmentDataSource.UpdateParameters.count is 3, and as you can see below dayAssignmentUpdate stored procedure has three parameters.
Code:
ALTER PROCEDURE [dbo].[dayAssignmentUpdate]
-- Add the parameters for the stored procedure here
@fcrID as int,
@Site as int,
@date as smalldatetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DELETE FROM [assignment] WHERE (assignment.FCR = @fcrID) AND (assignment.date = @date) AND NOT (@date = null)
END
IF NOT(@Site=13)
BEGIN
INSERT INTO [assignment] (assignment.FCR, assignment.site, assignment.date) VALUES (@fcrID, @Site, @date)
END
-
Aug 7th, 2009, 01:22 PM
#5
Re: Gridview update from stored procedure
OK so do those parameters have values against them?
-
Aug 7th, 2009, 01:28 PM
#6
Thread Starter
Addicted Member
Re: Gridview update from stored procedure
Im not sure how to see thier values. Ive been trying to get into them in the watch and immediate window but like I said, Im not sure where to look.
-
Aug 7th, 2009, 02:32 PM
#7
Fanatic Member
Re: Gridview update from stored procedure
I do not see any return value from the SP. And you have
Code:
<asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
I'm not sure what do you need it for but try commenting that line out and see if everything works fine then that would be the offending code.
-
Aug 7th, 2009, 03:45 PM
#8
Thread Starter
Addicted Member
Re: Gridview update from stored procedure
 Originally Posted by rjv_rnjn
I do not see any return value from the SP. And you have
Code:
<asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
I'm not sure what do you need it for but try commenting that line out and see if everything works fine then that would be the offending code.
Yeah thats fixed. Sorry I should have remembered to update this when I updated my code. The parameter count for this data source is correct at 3, so Im really confused how to even troubleshoot this.
-
Aug 7th, 2009, 04:46 PM
#9
Thread Starter
Addicted Member
Re: Gridview update from stored procedure
So as far as the problem with the number of arguments, I found this which solves that problem. I have 5 databound items in my gridview but was only passing 3 parameters to the update stored procedure. The first solution down solved this problem.
http://www.dotnetgoodies.com/article...specified.aspx
However I do not think the parameters are getting passed correctly. I am just trying to loop through each row in my gridview and update.
Code:
Dim i As Integer
i = 1
For Each row As GridViewRow In Me.assignmentGridView.Rows
Me.assignmentGridView.UpdateRow(i, False)
i = i + 1
Next
this now gives me an error:Cannot insert the value NULL into column 'FCR', table 'FCRschedule.dbo.assignment'; column does not allow nulls. INSERT fails.
The statement has been terminated.
The parameters I am trying to pass are definitly not null, so Im assuming they are not being passed properly.
-
Aug 8th, 2009, 04:30 AM
#10
Re: [RESOLVED] Gridview update from stored procedure
OK, so it appears that FcrID is null, for whatever reason, which in turn indicates that the GridView's SelectedValue property is null. Is there a reason you're using GridView's SelectedValue and then updating each row?
-
Aug 10th, 2009, 10:08 AM
#11
Thread Starter
Addicted Member
Re: [RESOLVED] Gridview update from stored procedure
So Ive had some progress. My page is working perfectly if the fcrID column is not hidden, when I hide it I get this error. Theres got to be a way to have this properly databind when the column is hidden. Any thoughts.
-
Aug 10th, 2009, 10:17 AM
#12
Fanatic Member
Re: [RESOLVED] Gridview update from stored procedure
When a column is hidden at server side, the HTML received by browser doesn't contain any definition for that column. So after a postback ASP.Net will always get a null value for row[column]. You will have to find a way to set a default value for fcrID in that case.
Maybe you can try setting style="visibility:hidden" for the column instead of doing a visible=false.
-
Aug 10th, 2009, 10:24 AM
#13
Thread Starter
Addicted Member
Re: [RESOLVED] Gridview update from stored procedure
that gives me a parser error.
-
Aug 10th, 2009, 10:53 AM
#14
Fanatic Member
Re: [RESOLVED] Gridview update from stored procedure
How did you add the style property to the gridview column? I tried it like below and works as expected and I can see the column name and values in page source.
Code:
<asp:BoundField DataField="TestName" ItemStyle-Width="10%" ItemStyle-CssClass="test" />
And the Css file has the class defined as
Code:
.test
{
visibility:hidden;
}
-
Aug 10th, 2009, 10:56 AM
#15
Thread Starter
Addicted Member
Re: [RESOLVED] Gridview update from stored procedure
Im just going to work around this by putting the invisible fcrID textbox in a visible column, which works for me. Thanks for all the help on this, you have no idea how much ive learned. I really wish I would have figured this stuff out a year or two ago, my old boss would have been much happier!
-
Aug 10th, 2009, 01:59 PM
#16
Re: [RESOLVED] Gridview update from stored procedure
Pfft, old people are never impressed
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|