Update value in Grid - asp.net

I have a grid that has some date values. What I like to do is to convert those date values into UTC date format and then enter that inside the table.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:KruSQL %>"
UpdateCommand="UPDATE [dbo].[Microbiology] SET RoomNum=#RoomNum, CollDate=#CollDate, WaterFixure=#WaterFixure Where ID=#ID">
<UpdateParameters>
<asp:Parameter Name="RoomNum" Type="String" />
<asp:Parameter Name="WaterFixure" Type="String" />
<asp:Parameter Name="CollDate" Type="DateTime" />
</UpdateParameters>
My qustion is how can I make use of :
UpdateCommand="UPDATE [dbo].[Microbiology] SET RoomNum=#RoomNum, CollDate=#CollDate, WaterFixure=#WaterFixure Where ID=#ID"
and still update the value of CollDate to UTC. Is there anyway to do this in the code behind and still make us of the UpdateCommand

try this :
UpdateCommand="UPDATE [dbo].[Microbiology] SET RoomNum=#RoomNum, CollDate=DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), #ColDate), WaterFixure=#WaterFixure Where ID=#ID"
convert Date to UTCDate :
DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), YOUR_DATE)

Related

SqlDataSource UpdateParameters - Input string was not in a correct format

I have inherited some ASP.NET code that I need to update which has resulted in my needing to change the ASP SqlDataSource's UpdateCommandType from a string (hard coded SQL Update statement) to a stored procedure containing the Update statement.
The string executes fine and uses parameters that are bound to controls in a details view (I know this is not best practice and it pains me having to work with data connections from the client side...! But I dont have time to re-write all the data connections for this and many pages just yet).
Anyway, I have just changed the UpdateCommand to a stored procedure that does the same thing and I just get the error
Input String was not in a correct format.
when I try to update on the page.
I can supply code if requested, but it is big & horrible so I am tentatively asking if anyone has any initial ideas? I will put a few small bits below though. I have been looking at the UpdateParameterCollection as I wonder if the parameter collection is getting cached anywhere - but cannot see anything.
I have controls bound to the DataSource items like so:
<EditItemTemplate>
<asp:CheckBox ID="chkNonReview" runat="server" Checked='<%# Bind("NonReview") %>' />
</EditItemTemplate>
And the SqlDataSource has been changed from this...
<asp:SqlDataSource ID="dsEventDV" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnString %>"
DeleteCommand="DELETE FROM [I_TRAINEE_EVENTS] WHERE [EVENTID] = #EVENTID"
InsertCommand="INSERT_TRAINEE_EVENTS_ED3"
InsertCommandType="StoredProcedure"
OnInserted="DSEvent_Inserted"
SelectCommand="Get_Candidate_Events_I3"
SelectCommandType="StoredProcedure"
UpdateCommand="UPDATE I_TRAINEE_EVENTS (etc...)"
.....
to this:
<asp:SqlDataSource ID="dsEventDV" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnString %>"
DeleteCommand="DELETE FROM [I_TRAINEE_EVENTS] WHERE [EVENTID] = #EVENTID"
InsertCommand="INSERT_TRAINEE_EVENTS_ED3"
InsertCommandType="StoredProcedure"
OnInserted="DSEvent_Inserted"
SelectCommand="Get_Candidate_Events_I3"
SelectCommandType="StoredProcedure"
UpdateCommand="UPDATE_TRAINEE_EVENTS"
UpdateCommandType="StoredProcedure">
.....
With the new UpdateCommand values.
The update parameters:
<UpdateParameters>
<asp:Parameter Name="EVENTID" Type="Int32"/>
<asp:Parameter Name="EVENTTYPEID" Type="Int32"/>
<asp:Parameter Name="EVENTDATE" Type="DateTime"/>
<asp:Parameter Name="STAFFID" Type="Int32"/>
<asp:Parameter Name="REVIEWNO" Type="Int32"/>
<asp:Parameter Name="COMMENTS" Type="String"/>
<asp:Parameter Name="DESTINYVERIFIED" Type="Int32"/>
<asp:Parameter Name="DESTINYVERIFIEDBY" Type="Int32"/>
<asp:Parameter Name="DESTINYVERIFIEDDATE" Type="DateTime"/>
<asp:Parameter Name="REASONFORSUSPENSIONID" Type="Int32"/>
<asp:Parameter Name="RETURNTOWORKDATE" Type="DateTime"/>
<asp:Parameter Name="ContactDetailsUpdated" Type="Int32"/>
<asp:Parameter Name="RETENTION_STATUS_ID" Type="Int32"/>
<asp:Parameter Name="RETENTION_REASON_ID" Type="Int32"/>
<asp:Parameter Name="NonReview" Type="Int32"/>
<asp:Parameter Name="FalsifiedEventReason" Type="Int32"/>
<asp:Parameter Name="SusReqReasonID" Type="Int32"/>
<asp:Parameter Name="SusReqReturnDate" Type="DateTime"/>
</UpdateParameters>
The stored procedure declaration is as follows:
CREATE PROCEDURE [dbo].[UPDATE_TRAINEE_EVENTS]
#EVENTID int,
#EVENTTYPEID int,
#EVENTDATE datetime,
#STAFFID int,
#REVIEWNO int,
#COMMENTS varchar(2100),
#DESTINYVERIFIED int,
#DESTINYVERIFIEDBY int,
#DESTINYVERIFIEDDATE datetime,
#REASONFORSUSPENSIONID int,
#RETURNTOWORKDATE datetime,
#ContactDetailsUpdated int,
#RETENTION_STATUS_ID int,
#RETENTION_REASON_ID int,
#NonReview int,
#FalsifiedEventReason int,
#SusReqReasonID int,
#SusReqReturnDate datetime
AS
......
I have also run SQL Server Profiler against the session to see what was being passed to the database, however the error comes in before anything hits the database which appears to suggest the problem is within the ASP.NET side of things.
Your <asp:Parameter Name="EVENTID" /> should have a datatype.
I think it should be like this..
<asp:Parameter Name="EVENTID" Type="Int32" />

ASP Gridview Select statement not working (GUID)

I'm using ASP.net to use a gridview. The gridview's sql data source is a select statement where ID = a value (a GUID) from a querystring.
However, when I try and preview it, it doesn't show any results. If I change it to a different type (int) of 1, then it works fine and shows results.
If I do the select statement in SQL Server Management Studio (with the GUID), it works fine and shows the results.
Here's the code for the data:
<asp:SqlDataSource ID="SQLDataSourceL" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT [FName], [Date] FROM [Table1] WHERE (([FName] = #FName) AND ([Type] = #Type)) ORDER BY [Date] DESC, [FName]">
<SelectParameters>
<asp:QueryStringParameter Name="FName" QueryStringField="id" Type="Object" />
<asp:Parameter DefaultValue="X" Name="Type" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
(I've changed the column names).
Hope you can help.
Try changing the QueryStringParameter to the suitable type, eg:
<asp:QueryStringParameter Name="FName" QueryStringField="id" Type="String" />

How to filter using a dropdown in a gridview and display all values when none is selected

I have a web page with 3 values (1 text box, and 2 drop-downs) and displays result in a gridview. This is working great when the value that I want to search is in the drop-down. However, if the user didn't select one of the dropdownlist ( facilityCode), we would like to display all facility codes versus only one. What is the easiest way to accomplish this? Below is the code related to the sqldatasource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:HCSCRMConnectionString %>"
SelectCommand="SELECT MMM_Id_Nbr, Item_Desc, Supplier_Nbr, Supplier_Name, Stocking_Facility_Code, Reorder_Point_Qty, BodID, Active_Ind FROM BOD_ROP_TBL WHERE (MMM_Id_Nbr = #MMM_Id_Nbr)and active_Ind=#Active_Ind and Stocking_Facility_Code=#FacilityCode"
UpdateCommand="UPDATE BOD_ROP_TBL SET Reorder_Point_Qty = #Reorder_Point_Qty, Active_Ind = #Active_Ind WHERE (BodID = #BodID)">
<SelectParameters>
<asp:FormParameter FormField="txt3MID" Name="MMM_Id_Nbr" Type="String" />
<asp:FormParameter FormField="dropActive" Name="Active_Ind" Type="String" />
<asp:FormParameter FormField="FacilityCode" Name="FacilityCode" Type="String" />
</SelectParameters>

ASP.NET how to pass Session variable in ListView

I'm trying to pass a Session("sessionVar") on to ListView INSERT. Is there any way I can achieve this. I tried this and dosns't seem to work:
InsertCommand="INSERT INTO [EmployeeTest] ([FName], [LName], [samAccount]) VALUES (#Fname, #LName,#samAccount)
<InsertParameters>
<asp:Parameter Name="FName" Type="String" />
<asp:Parameter Name="LName" Type="String" />
<asp:SessionParameter Name="EmployeeID" SessionField="samAccount"
Type="String" />
</InsertParameters>
On Page_Load:
Session["samAccount"]=getSamAccountFromActiveDirectory(UserName);
Am I missing anything? or Is it not possible to pass Session variable with in ListView.
Thank you.
Shouldn't you have EmployeeID as the db field name, not samAccount?
InsertCommand="INSERT INTO [EmployeeTest] ([FName], [LName], [EmployeeID]) VALUES (#Fname, #LName,#samAccount)
I found problem with this
in insert command you need to pass the name of parameter rather than name of session field
like as below
InsertCommand="INSERT INTO [EmployeeTest] ([FName], [LName],
[samAccount]) VALUES (#Fname, #LName,#EmployeeID)//changed from samAccount to EmployeeID

SWL Error Function has too many arguments specified

I am using ASP.NET 3.5 and SQL Server 2008.
I have a SQLDataSource & Gridview. I am trying to update 2 tables at the same time using a stored procedure in SQL.
SQLDatasource is passing 7 parameters.
5 parameters that the stored procedaure needs, Return value & StudentID.
Not sure if the error is in my SQLDatasorce or my stored procedure.
Here's my ASPX Code:
<asp:SqlDataSource ID="sqldsUserLoginNLevels" runat="server"
ConnectionString="<%$ ConnectionStrings:QuizStarConnectionString %>"
SelectCommand="SELECT UserLogins.StudentID, UserLogins.StudentName, UserLogins.UserID,
UserLogins.Password, UserLevels.GrammarStart, UserLevels.GrammarCurrent,
UserLevels.MathStart, UserLevels.MathCurrent
FROM UserLogins
INNER JOIN UserLevels ON UserLogins.StudentID = UserLevels.StudentID"
DeleteCommand="DELETE FROM [UserLogins] WHERE [StudentID] = #original_StudentID"
InsertCommand="INSERT INTO [UserLogins] ([StudentName], [UserID], [Password])
VALUES (#StudentName, #UserID, #Password)"
UpdateCommand="UpdateUserLoginsAndUserLevels"
UpdateCommandType="StoredProcedure" >
<DeleteParameters>
<asp:Parameter Name="original_StudentID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="StudentName" Type="String" />
<asp:Parameter Name="UserID" Type="String" />
<asp:Parameter Name="Password" Type="String" />
<asp:Parameter Name="GrammarStart" Type="String" />
<asp:Parameter Name="MathStart" Type="String" />
<asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="StudentName" Type="String" />
<asp:Parameter Name="UserID" Type="String" />
<asp:Parameter Name="Password" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
My Stored Procedure is:
ALTER PROCEDURE [dbo].[UpdateUserLoginsAndUserLevels] (
#StudentName VARCHAR(50),
#UserID NCHAR(10),
#Password NCHAR(10),
#GrammarStart NCHAR(10),
#MathStart NCHAR(10))
AS
DECLARE #StudentID INT;
BEGIN Transaction
BEGIN TRY
SELECT * From UserLogins
Where StudentID = #StudentID
UPDATE UserLogins
SET
StudentName= #StudentName,
UserID = #UserID,
Password = #Password
Where StudentID = #StudentID
UPDATE UserLevels
SET
GrammarStart= #GrammarStart,
MathStart = #MathStart
FROM UserLevels
INNER JOIN UserLogins ON UserLogins.StudentID = UserLevels.StudentID
WHERE (UserLevels.StudentID = #StudentID)
END TRY
BEGIN CATCH
DECLARE #ErrorMessage NVARCHAR(4000), #ErrorSeverity INT
-- Assign variables to error-handling functions that
-- capture information for RAISERROR.
SELECT #ErrorMessage = ERROR_MESSAGE(), #ErrorSeverity = ERROR_SEVERITY()
-- Rollback the failed transaction
ROLLBACK;
-- Raise an error: with the original error information.
RAISERROR(#ErrorMessage, #ErrorSeverity, 1);
END CATCH
COMMIT Transaction;
Have't figured how to post code yet. Sorry.
Make sure you are not double adding parameters. Since you are adding them in the aspx page you do not need to add them in your code behind.
For example if your code behind has this in it:
sqldsUserLoginNLevelsUserID.UpdateParameters.Add(new Parameter("UserID ", TypeCode.Int32));
It could explain your error.
If you need to set the value from code behind do it like this:
sqldsUserLoginNLevelsUserID.UpdateParameters["UserID "].DefaultValue = "1";

Resources