wildcard in SQLDataSource WHERE clause to show all results - asp.net

How can i have a wildcard in a where clause to where it will show ALL in a certain situation?
I have a dropdown list that contains:
All - (no value)
Staff (value = Staff)
Manager (value = Manager)
Distributor (value = Distributor)
My SQL select statement originally was:
SELECT * FROM [DB_Table] WHERE ([Site] = #Site) AND (RoleType=#RoleType)
The Role Type equals either Staff, Manager or Distributor and is never blank. This works great to filter by each of the 3 roles, but how can i make it so when ALL is selected, it shows all instead?
I believe i need to do something like this, but can't get it right:
SELECT * FROM [DB_Table] WHERE ([Site] = #Site) AND (RoleType=#RoleType OR #RoleType <> NULL)
ANSWER:
I couldn't leave the DDL value for ALL empty, so this is what it looks like and works now.
<asp:ListItem Text="All" Value="All" Selected="True"></asp:ListItem>
<asp:ListItem Text="Staff" Value="Staff"></asp:ListItem>
<asp:ListItem Text="Manager" Value="Manager"></asp:ListItem>
<asp:ListItem Text="Distributor" Value="Distributor"></asp:ListItem>
SELECT * FROM [DB_Table] WHERE ([Site] = #Site) AND (RoleType=#RoleType OR #RoleType = 'ALL')

SELECT * FROM [DB_Table] WHERE ([Site] = #Site) AND (RoleType=#RoleType OR #RoleType is NULL)
I think it should be null RoleType for the All drop down choice right?

Related

DataBinding: 'System.Data.DataRowView' does not contain a property with the name when using LEFT OUTER JOIN

For some reason I can not retrieve the value contained within 'CustomerAlertCountTotal'.
If I run the query using QueryBuilder (Visual Studio 2010) then the correct rows are returned, including a numerical count value/result for CustomerAlertCountTotal.
Here's my SELECT query:
<asp:SqlDataSource ID="SqlDataSourceCustomerList" runat="server"
ConnectionString="REMOVED"
ProviderName="REMOVED"
SelectCommand="SELECT contact.id_contact, contact.cname, contact.caddress1, contact.caddress2, contact.caddress3, contact.caddress4, contact.caddress5, contact.cpostcode, contact.ctel, contact.cmobile, contact.cemail, contact.trading_id, contact.cposition, trading_name.trading_name, CustomerAlertCountTotal FROM contact INNER JOIN trading_name ON contact.trading_id = trading_name.id_trading_name LEFT JOIN (SELECT COUNT(AlertID) AS CustomerAlertCountTotal, AlertTradingID FROM customer_support_dev.customer_alerts) AS COUNT ON COUNT.AlertTradingID = contact.trading_id WHERE (CHAR_LENGTH(contact.cname) > 0) ORDER BY contact.cname">
</asp:SqlDataSource>
Or slightly easier to read:
SELECT contact.id_contact, contact.cname, contact.caddress1, contact.caddress2, contact.caddress3, contact.caddress4, contact.caddress5, contact.cpostcode, contact.ctel, contact.cmobile, contact.cemail, contact.trading_id, contact.cposition, trading_name.trading_name, CustomerAlertCountTotal
FROM contact
INNER JOIN trading_name ON contact.trading_id = trading_name.id_trading_name
LEFT JOIN (SELECT COUNT(AlertID) AS CustomerAlertCountTotal, AlertTradingID
FROM customer_support_dev.customer_alerts) AS COUNT ON COUNT .AlertTradingID = contact.trading_id
WHERE (CHAR_LENGTH(contact.cname) > 0)
ORDER BY contact.cname
And this is how I'm trying to display the value:
<asp:TemplateField HeaderText="CustomerAlertCountTotal" SortExpression="CustomerAlertCountTotal">
<ItemTemplate>
<em><%#Eval("CustomerAlertCountTotal")%></em>
</ItemTemplate>
</asp:TemplateField>
If I replace this:
<em><%#Eval("CustomerAlertCountTotal")%></em>
With this:
<em><%#Eval("cemail")%></em>
Then I see their email address where the total CustomerAlertCountTotal value should be so I know the page code is essentially sound but for some reason, it appears that CustomerAlertCountTotal is not actually being selected as a usable value - yet it shows the count result fine in querybuilder??

asp.net dropdown null value stores 0 instead of null

I'm trying to add a title for dropdown menu.
The problem is that if the data is null, it automatically update as 0.
This doesn't happen when I create the data row.
The date type is int32.
The funny thing is that I can insert null but I can not update as null.
My solution was to add two listitems.
One is value="". This is for initial data. (create)
Another is value="0". This is for after update.
However this is not pretty solution.
How come integer stores 0 instead of null when I update the data?
It seems possible to store null when I create the row...
<asp:DropDownList ID="addl" runat="server" SelectedValue='<%# Bind("a") %>'>
<asp:ListItem Value="" Text="-Select-"></asp:ListItem>
<asp:ListItem Value="1">test1</asp:ListItem>
<asp:ListItem Value="2">test2</asp:ListItem>
<asp:ListItem Value="0" Text="-Select-"></asp:ListItem>
</asp:DropDownList>
what would be the best solution for dropdown menu title if the data type is integer?
If it's string, I can simply add value="" and works just time.
Integer stores 0 if it's null and I have to also create null in order to avoid error in edit mode.
Int32 is not a Nullable type and by default will equal 0 (even if not initialized). Try using Int32? which is Nullable, or use some other index to indicate the Create option.
I'm not quite sure what int you are trying to update, but if it's a variable that you have control over you could look at changing it from a standard int to a nullable int. This can be as simple as changing this:
int i;
to this:
int? i;

Sql Query Working in Management Studio but not in program

Consider the following query.
SELECT LP.project_id, LP.title, LP.type, LU.NAME, LP.reference, LP.status, LP.correspondence, LP.source, LP.deadline, LP.mstat, LP.num_pages, LP.done,
LP.ss_notes
FROM LogiCpsProjects AS LP LEFT OUTER JOIN
LogiCpsProjectAssignments AS LPA ON LP.project_id = LPA.project_id LEFT OUTER JOIN
LogiCpsUsers AS LU ON LPA.writer_id = LU.ID
WHERE (LP.status <> 'Closed') AND (LP.status <> 'Cancelled') AND (LP.type LIKE '%' + #type + '%') AND (LP.status LIKE '%' + #status + '%') AND
(LP.source LIKE '%' + #source + '%') AND (CAST(LP.mstat AS VARCHAR(50)) LIKE '%' + #mstat + '%')
ORDER BY LP.project_id
In this query I am trying to carry out a search that is based on four parameters namely #type, #status, #source and #mstat. The problem is this query works exactly as it should in Sql Server Management Studio for example if I enter 'Article' in type parameter and leave others blank then it returns all the records with type = 'Article' and if I enter type = 'Article' and status = 'Working' then it returns all records with type = 'Article' and status = 'Working' and so on... In simple words the search is dynamic as user can enter leave all parameters blank or put values in all four. It is working fine in MS but not in actual program.
I am using an SqlDataSource, with same query if I leave all parameters blank it does not return anything, if I enter 1 parameter it still does not return anything, it only return records if I enter all four parameters which means it is not working on blank parameters. To send parameters I am using four drop down lists. All of them look like this one.
<asp:DropDownList ID="ddlStatus" runat="server" Width="220px">
<asp:ListItem Selected="True" Value="" Text="">All</asp:ListItem>
<asp:ListItem>Details Pending</asp:ListItem>
<asp:ListItem>Working</asp:ListItem>
<asp:ListItem>Awaiting Feedback</asp:ListItem>
<asp:ListItem>Project Complete (AFB)</asp:ListItem>
<asp:ListItem>Revision Required Client</asp:ListItem>
<asp:ListItem>Revision Required Editor</asp:ListItem>
<asp:ListItem>To be Cancelled</asp:ListItem>
<asp:ListItem>Cancelled</asp:ListItem>
<asp:ListItem>Requirements Not Clear</asp:ListItem>
<asp:ListItem>Hold</asp:ListItem>
<asp:ListItem>Closed</asp:ListItem>
</asp:DropDownList>
The first item is defined as ALL and it has value and text set to "" (Empty string) so when user select ALL it means all records for that particular parameter.
What I am doing wrong ?
Pay attention to whether you're sending null or a blank string for all parameters. If you run the sql statement "select '%' + null + '%'" in management studio you'll find the result is null, which is not what you're looking for.

Bind check on column

I'm pretty new with VB & ASPX.NET.
But I'm having a problem with showing the right info in a gridview.
Basically I've got a maintenance tool for an application. And I want to make it multi language.
This is a check for the language:
'setting the column name where to get the text resource from
Dim comment As String = "comment"
If (licentie.getlanguage() = "NL") Then
comment = "comment_NL"
End If
'Part of the sql query
sqlDataSourceGridView.SelectCommand = "select inst.categorie,inst.term_id,inst.term_result,inst." + comment + ", ETC.....
So far this works. But in my template I've got the following code in the gridview:
<asp:Label ID="LabelType" runat="server" Text='<%# Bind("Comment") %>' />
So my question is, How do I set the column 'comment_NL' when the language is set 'NL' in the bind?
Just give a Column Alias for inst.comment and inst.comment_NL by using AS in your Select Query
select inst.categorie,inst.term_id,inst.term_result,inst." + comment + " AS Comment, ETC.....

set value of a gridview column depending on result from MS Access query

I have a query which is pulling values from a table in MS Access
SELECT
tblInstructor.InstructorID,
tblInstructor.Football AS InstructorRole,
tblInstructor.Gym AS InstructorRole,
tblInstructor.Yoga AS InstructorRole
FROM
tblInstructor
WHERE
tblInstructor.InstructorID = #InstructID
OR tblInstructorRole.Football = 1
OR tblInstructorRole.Gym = 1
OR tblInstructor.Yoga = 1
Then i do the databind to the gridview
In my aspx page im using the following method to construct my columns within the gridview
<asp:TemplateField HeaderText="Instructor ID">
<ItemTemplate >
<asp:Label ID="lblInstructorID" runat="server" Text='<%# Eval("InstructorID") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
I am having the issue here where I am getting the error of multiple returns, however each instructor has only one role, so I am trying to get the role of the instructor i.e. depending on whether it is true using my sql statement above and then set the instructor role column with the role of the instructor
<asp:TemplateField HeaderText="Instructor Role">
<ItemTemplate >
<asp:Label ID="lblInstructRole" runat="server" Text='<%# Eval("InstructorRole") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
My table has 3 separate field for Instructor Roles Football, Gym, Yoga which are true/false values in the Instructor timetable. I have been trying (with no luck) to get the column InstructorRole in the gridview to display the text of their role i.e.Football
I was also trying to do the following:
If myReader(3) = True then
Dim Role1 As String = DataBinder.Eval(e.Item.DataItem,"InstructorRole")
Role1 = "Football"
elseif myReader(4) = true then
Dim Role2 As String = DataBinder.Eval(e.Item.DataItem,"InstructorRole")
Role1 = "Gym"
If anyone could advise me what to do, I can't seem to figure this out.
You have to use a Case statement in your SQL query. Your Final query will look like:
SELECT
tblInstructor.InstructorID,
(case when tblInstructor.Football = 1 then tblInstructor.Football
case when tblInstructor.Gym = 1 then tblInstructor.Gym
case when tblInstructor.Yoga = 1 then tblInstructor.Yoga
else '' END) as InstructorRole
FROM
tblInstructor
WHERE
tblInstructor.InstructorID = #InstructID
If you can't modify the SQL query you could do it in code using Linq, something along the lines of:
var results = from p in [query results]
select new {
// other columns you need
InstructorRole = formatRole(p.IsFootball, p.IsGym, p.IsYoga )
}
and formatRole function is pretty straight forward
private string formatRole(bool football, bool gym, bool, yoga){
if( football )
return "Football";
else if( gym )
return "Gym";
else if( yoga )
return "Yoga";
else
// whatever the appropriate thing is, return "N/A" or throw an exception, up to you really.
}
You would then databind your grid to the results collection.

Resources