Sqldatasource where parameter - c#

I have a sqldatasource, in select command i have #status parameter. The parameter take the value from the textbox at runtime.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details] WHERE ([status] = #status)"
FilterExpression="title='{4}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
my problem is that when i run the page without entring the parameter in text box sqldatasource is not returing any row.

Looking at the documentation on MSDN, you have to alter how you have setup the SqlDataSource.
Try this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details]"
FilterExpression="title='{0}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
I have removed the Where clause from the query as this will be applied by the filter expression. Also, I have changed the filter expression from title='{4}' to title='{0}'. The documentation states that the number is a placeholder to an item in the FilterParameters collection.
Updated
I have also changed the SelectParameters to FilterParameters
Update 2
I have created a working example to finish of this answer. This will filter the Title column using the text from the text box. If this text box is empty it will return all the rows from the table (a scary thought but OK for this example). It is querying the AdventureWorks database for which I set a connection string called AWorks.
<asp:SqlDataSource ID="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:AWorks %>"
SelectCommand="SELECT ContactId, Title, FirstName, LastName FROM Person.Contact"
FilterExpression="Title='{0}'"
runat="server">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlID="txtTitle" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
<asp:TextBox runat="server" Id="txtTitle"></asp:TextBox>
<asp:Button runat="server" UseSubmitBehavior="true" Text="Submit" />
<asp:GridView
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:BoundField Visible="false" DataField="ContactId"></asp:BoundField>
<asp:BoundField Visible="true" DataField="Title"></asp:BoundField>
<asp:BoundField Visible="true" DataField="FirstName"></asp:BoundField>
<asp:BoundField Visible="true" DataField="LastName"></asp:BoundField>
</Columns>
</asp:GridView>

try a condition like this:
(#Status is null or #Status ='' Or Status = #Status)

set ConvertEmptyStringToNull="true" and then try....

Add CancelSelectOnNullParameter="false" to your SqlDataSource.

Related

How to fill dropdownlist from database based on selected item of another dropdownlist in asp.net

I have a SQL table named doctor search and have 2 dropdownlists one shows expertise of the doctor and i want to fill dropdownlist 2 with the information of those doctor which are related to that expertise section.
This can be done easily if you are using the designer interface.
Try using the below code after making necessary changes (Connection String, Table name and Column name). If that doesn't work, I will share the steps
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" SelectCommand="SELECT [Expertise] FROM [Your Table]"></asp:SqlDataSource>
<asp:DropDownList ID="ddlExpertise" runat="server" DataSourceID="SqlDataSource1" DataTextField="Expertise" DataValueField="Expertise">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" SelectCommand="SELECT [DoctorDetails] FROM [Your Table] WHERE ([Expertise] = #Expertise)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlExpertise" DefaultValue="" Name="Expertise" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="ddlDoctorDetails" runat="server" DataSourceID="SqlDataSource2" DataTextField="DoctorDetails" DataValueField="DoctorDetails">
</asp:DropDownList>

How to select cascading DropDownList with SqlDataSource

I have the following in my data base -
breedId Species Breed
0 dog Alsatian
1 dog pitbull
2 dog Shetland sheepdog
3 dog Boxer
4 cat Dragon Li
5 cat Australian Mist
6 cat Korat
In the c# designer view, I have 2 drop-down list one which has species and other for breed.
What I want is that when user picks 'dog' in species list,
the breed list should have the following Alsatian, pitbull, Shetland sheepdog,Boxer
At the moment when I pick 'dog', all the breed from the database is shown.
<asp:DropDownList ID="DropDownListSpecies" runat="server"
Height="27px" Width="107px" DataSourceID="hs330"
DataTextField="Species" DataValueField="Species">
</asp:DropDownList>
<asp:SqlDataSource ID="Species" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [Species] FROM [Breed]">
</asp:SqlDataSource>
<asp:DropDownList ID="DropDownListBreed" runat="server" Height="20px"
Width="110px" DataSourceID="breed" DataTextField="Breed"
DataValueField="Breed">
</asp:DropDownList>
<asp:SqlDataSource ID="breed" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [Breed] FROM [Breed]">
</asp:SqlDataSource>
You need to use ControlParameter in SelectParameters.
Make sure that AutoPostBack="True" for DropDownListSpecies
FYI: You have typo in Speecies
<asp:DropDownList ID="DropDownListSpecies" runat="server"
Height="27px" Width="107px" DataSourceID="Species"
DataTextField="Species" DataValueField="Species" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="Species" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [Species] FROM [Breed]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownListBreed" runat="server"
Height="20px" Width="110px"
DataSourceID="breed" DataTextField="Breed" DataValueField="Breed">
</asp:DropDownList>
<asp:SqlDataSource ID="breed" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [Breed] FROM [Breed] WHERE Species=#Species">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListSpecies" PropertyName="SelectedValue"
Name="Species " Type="String" DefaultValue="cat" />
</SelectParameters>
</asp:SqlDataSource>
You are not filtering the data in the second drop down list, based on the selection made in the first drop down list (which is what you want).
<asp:DropDownList ID="DropDownListBreed" runat="server" Height="20px" Width="110px" DataSourceID="breed" DataTextField="Breed" DataValueField="Breed">
</asp:DropDownList>
<asp:SqlDataSource ID="breed" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT DISTINCT [Breed] FROM [Breed] WHERE Species = #Species">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListSpecies" PropertyName="SelectedValue"
Name="Species " Type="String" DefaultValue="cat" />
</SelectParameters>
</asp:SqlDataSource>
Also, you need to add the AutoPostBack="True" property to each DropDownList, if you want the changes to be reflected as soon as you change the value of each DropDownList.

Create Grid view with master page in asp

i am new in asp, i create a grid view with master page. but it show an error in connection string. can anyone help me please?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
AutoGenerateEditButton="true" AutoGenerateDeleteButton="false"
DataSourceID="SqlDataSource1" Font-Names="Arial" Font-Size="Smaller" DataKeyNames="Logid" >
<columns>
<asp:BoundField DataField="Logid" HeaderText="Logid" SortExpression="Logid" />
<asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" />
<asp:BoundField DataField="PASSWORD" HeaderText="PASSWORD" SortExpression="PASSWORD" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
<asp:BoundField DataField="E_MAIL" HeaderText="E_MAIL" SortExpression="E_MAIL" />
</columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"runat="server" ConnectionString="<%$ ConnectionString:SaqibConnectionString %>"
SelectCommand="SELECT [Logid], [Username], [PASSWORD], [Time_Logged_in], [Time_Logged_Out], [Status], [Date_Logged_in], [E_MAIL]"
UpdateCommand="Update Log_Users SET Logid=#Logid, Username=#Username, PASSWORD=#PASSWORD, Status=#Status, E_mail=#E_mail"
DeleteCommand="DELETE FROM Log_Users WHERE Logid = #Logid">
<UpdateParameters>
<asp:Parameter Name="Logid" />
<asp:Parameter Name="Username" />
<asp:Parameter Name="PASSWORD" />
<asp:Parameter Name="Status" />
<asp:Parameter Name="E_MAIL" />
</UpdateParameters>
</asp:SqlDataSource>
</asp:Content>
Are you sure it's not something simple like the space between ID="SqlDataSource1" and runat="server" bit.
This would cause the error "Server Tag is not well formed" error.
Connection string name should be match with what you have given in web config so,
change ConnectionString="<%$ ConnectionString:SaqibConnectionString %>" to
ConnectionString="<%$ ConnectionStrings:db %>"
or change the name in configuration file which given to your connection string as SaqibConnectionString
Update:
ConnectionString="<%$ ConnectionString:SaqibConnectionString %>"
/\
you missed `s` here
You made mistake on Connection String setting, it should be ConnectionStrings, note that s at the end
try below
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [Logid], [Username], [PASSWORD], [Time_Logged_in], [Time_Logged_Out], [Status], [Date_Logged_in], [E_MAIL] from Log_Users"
UpdateCommand="Update Log_Users SET Logid=#Logid, Username=#Username, PASSWORD=#PASSWORD, Status=#Status, E_mail=#E_mail"
DeleteCommand="DELETE FROM Log_Users WHERE Logid = #Logid"
ConnectionString="<%$ ConnectionStrings:db %>">
<DeleteParameters>
<asp:Parameter Name="Logid" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Logid" />
<asp:Parameter Name="Username" />
<asp:Parameter Name="PASSWORD" />
<asp:Parameter Name="Status" />
<asp:Parameter Name="E_MAIL" />
</UpdateParameters>
</asp:SqlDataSource>
As seen above your connectionstring name is db but you are using SaqibConnectionString as connection string.so use ConnectionString="<%$ ConnectionString:db %>" in place of
ConnectionString="<%$ ConnectionString:SaqibConnectionString %>"
Update1
and your select statement is not right
SelectCommand="SELECT [Logid], [Username], [PASSWORD], [Time_Logged_in], [Time_Logged_Out], [Status], [Date_Logged_in], [E_MAIL]"
select statement should be
select [Logid], [Username], [PASSWORD], [Time_Logged_in], [Time_Logged_Out], [Status], [Date_Logged_in], [E_MAIL] from Log_Users you are missing from tablename in your select statement
Update2
you do not have space between ID="SqlDataSource1" and runat="server"

using session variable in aspx page in sql query

I have the sqldatasource in aspx page and in the query I want to use one parameter i.e. coming from session.
Below is my code.Please help me out.
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id="Here I want to use session variable"" />
Try this
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id=#SessionVar">
<SelectParameters>
<asp:SessionParameter Name="SessionVar" SessionField="SessionVariableName" ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>
This MSDN article should get you what you need. Basically you would define your SelectCommand with the parameter placeholder, "?", and then define your SelectParameters collection with an entry for your SessionParameter.
Using parameters is rather simple:
<asp:SqlDataSource id="Employees" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind%>"
SelectCommand="SELECT LastName FROM Employees WHERE Title = #Title">
<SelectParameters>
<asp:ControlParameter Name="Title"
ControlID="DropDownList1"
PropertyName="SelectedValue"/>
</SelectParameters>
</asp:sqldatasource>
Just replace the value of parameter with your variable:
<%= Sessiom[variable_name] %>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=LAZY-PC;Initial Catalog=Test;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [C] WHERE ([C#] = #column1)">
<SelectParameters>
<asp:SessionParameter Name="column1" SessionField="id" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Johnny_D's answer is pretty much spot on, but I'd like to point out that there's a SessionParameter class that you can use for this:
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id= ?" />
<SelectParameters>
<asp:SessionParameter
Name="id"
SessionField="SessionVariableName"
DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>

ORA-01008: not all variables bound (Delete Event on ASP.net Gridview)

I am currently experiencing a really weird problem with deleting a row from a GridView. I have the following code, and it throws the following exception:
ORA-01008: not all variables bound
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="DeleteBTN" runat="server" OnClientClick="return confirm('Are you sure you want to delete this move request?')" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UNIQUEKEY" HeaderText="Unique Key"
SortExpression="UNIQUEKEY" />
<asp:SqlDataSource ID="GridViewDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:Oracle1ConnectionString %>"
ProviderName="<%$ ConnectionStrings:Oracle1ConnectionString.ProviderName %>"
SelectCommand="SELECT ORIGINATOR, TO_CHAR(REQUESTDATE, 'MM/DD/YYYY') as REQUESTDATE, PARTNUMBER, REQUESTQTY, MOVEFROM, MOVETO, COMPLETEDBY, TO_CHAR(COMPLETION_DATE, 'MM/DD/YYYY') as COMPLETION_DATE, COMMENTS, RESPONSETIME, PROCESS_FLAG, UNIQUEKEY FROM MATERIALMOVEREQUEST WHERE RTRIM(PROCESS_FLAG) = :ProcessFlag ORDER BY REQUESTDATE DESC"
DeleteCommand="DELETE FROM MATERIALMOVEREQUEST WHERE UNIQUEKEY = :UNIQUEKEY">
<SelectParameters>
<asp:ControlParameter ControlID="RequestTypeLabel" DefaultValue="" Name="ProcessFlag" PropertyName="Text" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="UNIQUEKEY" Type="Decimal" />
</DeleteParameters>
UNIQUEKEY is a 'Number' field in my Oracle table and I have tried all kinds of data types where it is currently a 'decimal' type.
Use Deleting event of SqlDataSource and try writing following code:
e.InputParameters["UNIQUEKEY"] = value;
value is what you want to send to database
Fixed it myself, had to add the primary keys to the GridView parameters (DATAKEYNAMES). Here is my code snippet:
<asp:GridView ID="MMRGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="UNIQUEKEY"
DataSourceID="GridViewDataSource" Width="980px" OnRowDataBound="GridViewRowEventHandler"
EmptyDataText="No Data Found" AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="Delete" Visible = "true">
<ItemTemplate>
<asp:LinkButton ID="DeleteBTN" runat="server" OnClientClick="return confirm('Are you sure you want to delete this move request?')" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Categories

Resources