I have Ref table and patientDetails in sql. for example in Ref I have Gender column with values (Female, Male etc), In patientDetails the Gender column is 24 and I am binding the data to patientDetails and I have 24 in the Gender column in the Gridview but it should be Female. ( Foreign key and navigation properties would not work because I have so many different rows in Ref table like ethnicity,religion etc). I was told RowDataBound will work so I have this method now but its not going past the If statement line..
More Details:
my Data Source is binding to PatientDetails table which has Gender column ( with number values - which is getting it from ref table). I mean I have Ref table ( Like a lookup for everything - with columns ID, name, Description) In name I have gender, Ethnic group, religion) In Description Column I have the values for instance for gender: male, female etc however in the Gender column in Patient is returning the corresponding Ref ID of the value - for female it will store 24 in Gender column in Patient) so my Gridview is bounding correctly and getting whats stored in PatientDetails table however having 24, 25 in Gender is not helpful to show so I was told RowDataBound Can bring the correct correponding value in Ref table
Protected Sub gvPatientDetails_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblGender As Label = e.Row.FindControl("lblGender)
End If
End Sub
<asp:GridView ID="gvPatientDetails" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvPatientDetails_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="lblGender" runat="server" Text='<% #Eval("lblGender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
OK, I got it working.
For all the beginners who might struggle with this, the above code is working as it should, when I debug it first time it just hit the IF statement then doesnt go inside ( because the first one is Header (I know duh!!) hit continue and it should give you the actual value of label then after that it should be simple. Just getDataFromRef ( a method to get data from Ref - id = lblGender ) then
lblGender.Text = ( The value from method - getDataFromRef ).
Basically I wasn't aware the debugger will start with the Header then when you hit continue will go through the gridview rows!
All the best all, happy coding!:)
Related
I have a grid view, which displays the monthly items purchased by our registerd dealers. On button click event, all the items displayed in the grid view row wise should be made purchased by the dealer on one single click. This is the case of bulk update.
grid view code::
<asp:GridView ID="GridCustomer" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="PURCHASED_MONTH" HeaderText="MONTH" ></asp:BoundField>
<asp:BoundField DataField="ITEM_NAME" HeaderText="ITEM NAME"></asp:BoundField>
<asp:BoundField DataField="PAYABLE_AMOUNT" HeaderText="PAYABLE AMOUNT"></asp:BoundField>
</Columns>
</asp:GridView>
The number of rows is dynamic, sometimes single row sometimes multiple.
The table data ::
ID MONTH ITEM_ID STATUS PAYABLE_AMOUNT
1 4 155 1
2 5 159 1
3 6 256 1
In the UPDATE statement the WHERE CLAUSE will be ID. According to cases, I will have single ID or multiple IDs. For example,
update tbl_wholesome_purchase set STATUS = 2 WHERE ID = 1 but in some case ID = 1, 2, 3... also.
How can I create such update case stored procedure in which the WHERE CLAUSE parameter is dynamic ?
Are you using SQL Server 2016 or newer ?.
In that case you can use the string_split function to get a very easy solution, passing the IDs as a single comma-separated string.
DECLARE #ids varchar(max) = '4,7,9,25'
UPDATE tbl_wholesome_purchase SET status = 2
WHERE id IN (SELECT value FROM split_string(#ids, ','))
Another solution is to pass the ids already as a list, on table valued parameters. You will need to follow the documentation of ASP.NET about how to call them on dot net.
CREATE TYPE ListIDs AS TABLE ( id int );
GO
CREATE PROCEDURE dbo. update_wholesome_purchase
#ids ListIDs READONLY
AS
BEGIN
UPDATE tbl_wholesome_purchase SET status = 2
WHERE id IN (SELECT id FROM #ids);
END
GO
SQL Server: I have 3 columns [ID], [Text], [Value]
ASPX page:
I have a dopdownlist populated by a SqlDataSource:
<asp:DropDownList ID="ddl" runat="Server" DataSourceID="sql" DataValueField="ID" DataTextField="Text" />
<asp:SqlDataSource ID="sql" runat="Server" SelectCommand="sp" SelectCommandType="StoredProcedure" />
Using DataValueField and DataTextField I can capture 2 of the values in the SQL Server table based on selected value.
Question:
How to capture the additional column [Value] based on the users #ddl selected value?
Notes:
I can achieve this using javascript to populate a matching dropdown list using the [Value] field instead of the [ID] field and match on [Text] field.
There must be a more efficient and appropriate way to handle this with just asp or VB.
ANSWER USED
I simply got the value at time of use using the ddl [ID] and a sql select statement.
SELECT #Value = [Value] FROM [tbl] WHERE [ID] = #ID
Passing in #ID as the dropdownlist selectedvalue.
I'm guessing the ID is a unique identifier of the row, so why would you need the text or value columns to be submitted by the form? You can just retrieve these from the database using the ID that is submitted. Or am I missing something here?
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??
I have a grouped LINQ query with a count summary line that I am binding to a gridview but the count field in the gridview just shows 'System.Data.DataRow[]'
What am I doing wrong here? Thanks!
aspx:
<asp:BoundField DataField="Service_Name" HeaderText="Impacted services"/>
<asp:BoundField DataField="Fieldname" HeaderText="Count"/>
vb code:
Dim Services = From Lines In dtMain _
Where Lines.Field(Of String)("ULTIMATE_PARENT_NAME") = lstUPs.SelectedItem.ToString _
Group By Parent_Name = Lines.Field(Of String)("ULTIMATE_PARENT_NAME"), _
Service_Name = Lines.Field(Of String)("THIRD_PARTY_SERVICE_CLEAN") _
Into Fieldname = Group, Count()
grdOuterGridView.DataSource = Services
grdOuterGridView.DataBind()
I'm not an expert on that syntax (I use the extension methods) but it looks to me like Fieldname is being assigned the value Group, or the collection of rows that are being grouped on Parent_Name and Service_Name. Perhaps you need to define a field name for the result of Count()?
ETA: Got it. You should say Group.Count(), not Group, Count().
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.