Bind check on column - asp.net

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.....

Related

Paging error with EntityDataSource

I'm using an EntityDataSource and creating a projection using the CommandText property to query across several tables. I want to allow paging, but when I run the code I get an error that says
For the EntityDataSource, if the query specifies a projection and
paging is enabled, a sort expression must be defined. Either set the
OrderBy property or set AutoGenerateOrderByClause to true
Odd thing is, I HAVE set the AutoGenerateOrderByClause to true and the error persists. I'm not sure what to do at this point. Here's my example EntityDataSource code.
<asp:EntityDataSource runat="server" ID="EntityDataSource"
ConnectionString="name=AssetRegistryEntities"
DefaultContainerName="AssetRegistryEntities"
CommandText="SELECT a.astName, a.astDescription, r.rolFK_adCN
FROM AssetRegistryEntities.Assets as a
JOIN AssetRegistryEntities.Roles as r ON r.rolFK_astID = a.astID
WHERE r.rolFK_adCN = 'dpellerin'
AND r.rolTypeCode = 'PRIANALYST'"
AutoGenerateOrderByClause="true">
</asp:EntityDataSource>
Anyone know how to make paging work with this?
You have a projection. Either get rid of the SELECT or also add OrderBy="it.astID"
Just to clarify the accepted answer of AFD.
Change the EntityDataSource to the following:
<asp:EntityDataSource runat="server" ID="EntityDataSource"
ConnectionString="name=AssetRegistryEntities"
DefaultContainerName="AssetRegistryEntities"
OrderBy="it.astID"
CommandText="SELECT a.astName, a.astDescription, r.rolFK_adCN
FROM AssetRegistryEntities.Assets as a
JOIN AssetRegistryEntities.Roles as r ON r.rolFK_astID = a.astID
WHERE r.rolFK_adCN = 'dpellerin'
AND r.rolTypeCode = 'PRIANALYST'"
AutoGenerateOrderByClause="true">
</asp:EntityDataSource>

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.

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.

How to solve this error "String was not recognized as a valid DateTime "

Hello
I am using datetime datatype in sql sever but whenever i save this form i am getting this error "String was not recognized as a valid DateTime" . In my code i am using ajax code on textbox to select date . But same error i am getting . I am specify date format into it but still it get error . What we do now.
Try Format:
DateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
you can use this vb code to convert it :
dataandtimevarible.Value.ToString("dd-MM-yyyy")
you need to add Format="dd-MM-yyyy" to your mark up code and add this one to your markup Code
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd-MM-yyyy" TargetControlID="CreatedOnTextBox">
</ajaxToolkit:CalendarExtender>
last thing you have to make sure from the Culture language!!!
How are you saving your data into the database? Are you using parameters? If not then use parameters - to save date/time value, choose your parameter data type as DbType.DateTime.
Now, your text box will give you a string value and you need to convert it to date/time data type before assigning to the parameter. Use DateTime.ParseExact method with your specific date format - for example,
var param = new SqlParameter("MyDateColumn");
param.DbType = DbType.DateTime;
param.Value = DateTime.ParseExact(textbox1.Text, "dd-MM-yyyy", null);

Passing Multiple values Through Query String?

I tried to pass more than one value through Query String from page1.aspx to page2.aspx.
This is my Query string in the Grid View
<a href="javascript:void(0);" onclick='javascript:window.open("Update.aspx?Regno= <%#Eval ("ID") %>'+ ","'&Fn=<%#Eval ("FIRSTNAME") %>' +", "'&Ln=<%#Eval ("LASTNAME") %>'")';>
Edit</a>
On My Page2.aspx, my code behind on PageLoad is:
if (Page.IsPostBack) return;
string id = Request.QueryString["ID"];
string Firstname = Request.QueryString["FIRSTNAME"];
string LastName = Request.QueryString["LASTNAME"];
My Visual Studio IDE shows a syntax error on this query string. I dont know the exact way to pass multiple values through Query String. How to make it work? Can anyone pls help me on this..
Which is the right syntax to pass multiple query string?
You use the & to seperate multiple query string cars. For example, Foo=12&first=death
("LASTNAME") %>'")' ;
Whats up with the semicolon here at the end???
try removing that as u dont need it
Also
You string is a bit difficult to find any missing , quotes. Better print in console , or give an alert for this
Update.aspx?Regno= <%#Eval ("ID") %>'+ ","'&Fn=<%#Eval ("FIRSTNAME") %>' +", "'&Ln=<%#Eval ("LASTNAME") %>'")';
And copy the printed string, paste it in the Browser and check whether it works! There by u can see what are all the things getting passed and where you made mistake in syntax

Resources