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
Related
I have an Editable Interactive Grid (for product_sale table) with a Select List (to select a product) in Oracle Apex 20.2 app.
Currently I'm using below query to populate this list of values.
SELECT productName,productId FROM product WHERE productAvailability = 'Y'
There are a few products that I need to set the productAvailability as 'N'. When I made this change, Interactive Grid of product_sale shows productId instead of the productName.
What I need to achieve is, only show products with productAvailability = 'Y' for new records (when going to add new record in to the table by clicking Add Row button) and for the old records show the productName of the selected product regardless the productAvailability.
Table Structure
Sample Data
Interactive Grid View
How could I achieve this?
You can achieve this using the "cascading list of values" option in the column settings. Let me illustrate with an example on the EMP sample table - you should be able to translate this to your own code:
Situation: BLAKE can not be selected as manager for any other employees than the one he is already manager for.
This would be the select:
SELECT ename, empno FROM emp WHERE ename != 'BLAKE'
As expected, for records that have BLAKE as manager, the MGR column will display the id because the value is not in the result set of the select list query. This is the behaviour you are seeing.
The solution has 2 steps:
Union the query with a query that has the value of the current row. I have rewritten the query using a CTE.
WITH all_mgr(dv, rv) AS
(SELECT ename, empno FROM emp WHERE ename != 'BLAKE'
UNION
SELECT m.ename, m.empno
FROM emp e
JOIN emp m ON e.mgr = m.empno
WHERE e.ename = :ENAME
)
SELECT dv, rv FROM all_mgr
Make sure column ename from the current row is bind to :ENAME. In "Cascading List Of Values" set both "Parent Column" and "Items to Submit" to ENAME. Now the select list will take the current row value for :ENAME and the list of values will include 'BLAKE' for users that have 'BLAKE' as a manager.
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!:)
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?
student code| student.Name |Region Name|Location Name| Assets Details |
I have to display asset details on the basis of each student. There can be more than one asset details for one student, how can i use this in repeater or gridview.
Table which i have in SQL.
Student Code | student name | Asset Details
1 Ray laptop
1 Ray Bed
2 Raj Phone
2 Raj charger\
....
I want to display asset details for each unique student code like this on my page.
Student Code | student name | Asset Details
1 Ray laptop
Bed
2 Raj Phone
Charger
How do i perform the same with only one hit from sql.?
You can do the following:
Use a stored procedure to return two results set for the following queries
SELECT DISTINCT StudentCode, studentname FROM StudentInfo
SELECT StudentCode, student name, Asset Details
Create a DataSet for the results with 2 tables. You can name the first table "Student" and the second table "StudentInfo"
Create a relationship for the tables (please use the intellisense for syntax)
DataSet.Relationships.Add("RelationshipName",Table1.StudentCode,Table2.StudentCode)
Bind the GridView with the DataSet
Create a Template column for Asset Details and insert a repeater or Gridview for that column.
On the ItemDataBound event of the GridView.
DataRowView drv = (DataRowView)e.ItemData;
Repeater.DataSource = dv.CreateChildView();
Instead of using two repeater control, try to make a sql query to get data in this format
use such query
select studentID,
stuff((select ',' + AssetDetails
from Tbl_Student t2
where t2.studentID= t1.studentID
order by studentID
for xml path('')),1,1,'') as AssetDetailsString
from Tbl_Student t1
group by studentID
this will return single row for every studentID with Assestdetails in CSV format in same row
i have two textboxes 1 button and 1 gridview and 1 button
database structure :
ID NAme Age date
1 sumit 24 1-Mar-2011
2 Manish 22 5-Mar-2011
what i want to do...
i have to text boxes textbox1 in which user enter from date and in textbox2 user enetr to date and after button clickevent the gridview will display the record between fromdate and to date..
mysql query ..
SELECT Ticket_no, journey_date, Travels, route, sel_seat, seat_opt, net_pay, name, mob, book_date, PNR_no FROM a1_ticket WHERE journey_date BETWEEN journey_date1 AND journey_date2 ORDER BY ID DESC
journey_date1 =textbox1
journey_date2 =textbox2
whats wrong in this query ?
ERREO : It will notdisplay record according to query ..
Change
orderby
to
order by
not sure if this will fix all your problems, or if that syntax is actually valid.
I think in the Query, there may be problem in Converting the Datetime to the format you specified in your table.