ASP Gridview Select statement not working (GUID) - asp.net

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" />

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" />

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>

Control doesn't bind to SqlDataSource on page load with blank parameters

Suppose you have a SQLDataSource that looks like this:
<asp:SqlDataSource ID="sqldsSample" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT [col1], [col2] FROM [tbl] WHERE [col3] = #val) ORDER BY [col1] DESC;">
<SelectParameters>
<asp:Parameter DefaultValue="False" Name="val" Type="Boolean" />
<asp:Parameter DefaultValue="" Name="val2" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Just assume you have decided you want to declare the 'val2' parameter here and you don't want to bother with adding and/or removing parameters later in the code-behind (say, to change the SelectCommand to do some filtering with some extra criteria).
It will fail without an error - the control will just show up empty.
You have to specify a default value for the parameter even if it's not used. For example, just putting in a space character will work:
<asp:Parameter DefaultValue=" " Name="val2" Type="String" />
Note that it still won't work if you omit the DefaultValue property.
There is also the CancelSelectOnNullParameter parameter on SqlDataSource - you need to set this to false if you expect any of your parameters to be null.
It's not very obvious and it has caught me out a few times!

How to set parameters for SqlDataSource UpdateCommand

For a Gridview:
I am trying to use a stored procedure for the first time in a SQLDataSource for the UpdateCommand:
<asp:SqlDataSource ID="TECT_DataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:OracleConnectionString %>"
ProviderName="<%$ ConnectionStrings:OracleConnectionString.ProviderName %>"
SelectCommand="SELECT MPID, User_Id, Last_Name, First_Name
FROM Scripts.vw_Tect_Exam"
UpdateCommand="P_TECT_UPD_EXAM_ID" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="MPID" Type="Int32" />
<asp:Parameter Name="User_Id" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
I am wondering how the UpdateParameters get their values set, since I only specify a name?
The procedure P_TECT_UPD_EXAM_ID expects two parameters as input: "in_MPID" and "in_UserId"
I am also wondering how to map those values to the input parameters for the procedure as the names are different?
You can set them something like this :
Example : In the example MPID is the sql parameter name #MPID
<UpdateParameters>
<asp:ControlParameter Name="MPID" ControlID="MPID_TextBox" PropertyName="Text />
<asp:ControlParameter Name="User_Id" ControlID="User_Id_TextBox" PropertyName="Text />
</UpdateParameters>
Correction: Just spotted your proc param names so it must be
<asp:ControlParameter Name="in_MPID" ...............
<asp:ControlParameter Name="in_User_Id" ...............
Hope this helps....
I really wouldn't use a SqlDataSource. It will be much easier if you make the call to the database in the code-behind (or a better yet in a Data Access Layer).
If you use a SqlDataSource the stored procedure call will only be available on that page. Every time you want to make that same call you will have to copy and paste the SqlDataSource or make a UserControl out of it.
The following example uses the Entity Framework to connect to the database and retrieve records:
public List<Record> GetAllRecordsByUserName(string credentials)
{
List<Record> recordList;
using (CustomEntities context = new CustomEntities())
{
IQueryable<Section> recordQuery = from records in context.Records
where records.UserName == credentials
select records;
recordList = recordQuery.ToList<Record>();
}
return recordList;
}

Asp .NET SelectCommand Dynamic Column Names

I have a database with two columns: Retailers or Traders. A row can have only one of them with values while the other must be 'Null'. Both are nvarchars kinds. What I am trying to do is to show data if a radio button is selected for either Retailer or Trader. So I constructed a SelectCommand statement which is getting column names of either 'Retailers' or 'Traders' as a parameter but the SelectCommand statement still returns all records. What can I do?
Thanks!
<asp:SqlDataSource ID="dsSearchResults" runat="server" ConnectionString="<%$ ConnectionStrings:AdoFabricsConnection %>"
SelectCommand="SELECT Name,Address1,City,State,Zip, Telephone, Email_Address, TO_THE_Trade, Retailer, Latitude, Longitude, SQRT(POWER([Latitude] - #Latitude, 2) + POWER([Longitude] - #Longitude, 2)) * 62.1371192 AS DistanceFromAddress FROM [ado_DealerLocator] WHERE (#RetTradColumnName IS NOT NULL) AND (ABS([Latitude] - #Latitude) < #LatLongUnitsParam) AND (ABS([Longitude] - #Longitude) < #LatLongUnitsParam) ORDER BY DistanceFromAddress">
<SelectParameters>
<asp:Parameter Name="Latitude" />
<asp:Parameter Name="Longitude" />
<asp:Parameter Name="LatLongUnitsParam" />
<asp:Parameter Name="RetTradColumnName" />
</SelectParameters>
</asp:SqlDataSource>
Never mind. I ended up combining the values of Traders and Retailers values into one new database field 'RetailerOrTrader' and programmed accordingly.
Thanks.

Resources