How to get XML value from a column in SQL Server - asp.net

I have a SQL Server table tblApplications with some columns... one of the columns is called Content and has XML values like in the following Image:
when i clicked on the value of content in the above image it displays like following in new tab
I want to get the value using id from the xml value of dataitem that is under the datagroupItem of datagroup as selected in following image, using query from the tblApplications
How to get the value from the content using id?? E.g. I want to get the value of id of dataitem = 'ForeNames'
How to get it using query????

You could try this query:
SELECT a.application_id,
x.y.query('.') AS DataItemNode,
x.y.value('(#type)[1]','NVARCHAR(50)') AS TypeAttr,
x.y.value('(#value)[1]','NVARCHAR(50)') AS ValueAttr
FROM dbo.tblApplications a
CROSS APPLY a.Content.nodes('/XmlDataPairDocument/dataitem/datagroup/datagroupitem/dataitem[#id="forenames"]') x(y)
or
DECLARE #id NVARCHAR(50);
SET #id='forenames';
SELECT a.application_id,
x.y.query('.') AS DataItemNode,
x.y.value('(#type)[1]','NVARCHAR(50)') AS TypeAttr,
x.y.value('(#value)[1]','NVARCHAR(50)') AS ValueAttr
FROM dbo.tblApplications a
CROSS APPLY a.Content.nodes('/XmlDataPairDocument/dataitem/datagroup/datagroupitem/dataitem[#id = sql:variable("#id")]') x(y)
Another similar questions-answers: here #1, here #2

Related

asp.net SqlDataSource select statement with IN (...) clause

I need to pass a server-side parameter to my SqlDataSource SELECT IN-clause as follows (notice the parameter #InClause and it's location in the SQL-select that is defined in the aspx (for the SqlDataSource):
SELECT UID_REPORT, TXT_RPT_TEXT, TXT_RPT_NAME, TXT_RPT_ASPX_PAGE, TXT_RPT_TYPE
FROM REPORTS WHERE (UID_REPORT IN (#InClause))
ORDER BY INT_SORT_ORDER
But this does not pass validation in the Test-Query operation.
The '#InClause' parameter gets it's value from a HiddenField control.
I want to set this value based on some conditions. for example If DIVISION=5 then #InClause would ="45,46" Else ="14,15".
Any suggestions on how to pass the IN ( ... ) values by the hiddenfield.Value (which is a string, of course).
Thanks.
You need split string function to do this
SELECT uid_report,
txt_rpt_text,
txt_rpt_name,
txt_rpt_aspx_page,
txt_rpt_type
FROM reports
WHERE uid_report IN (SELECT split_value
FROM Udf_splitstring(#InClause, ','))
ORDER BY int_sort_order
Check the below links to create a split string function
http://sqlperformance.com/2012/07/t-sql-queries/split-strings
http://www.sqlservercentral.com/articles/Tally+Table/72993/
or of-course you can use dynamic sql

Stored Procedure value to asp.net page under text box

I have a store procedure which show single row value like
(x.CLTotalAc, x.CLTotalAmt,x.CLAc, x.CLAmt,x.CL,
y.GB_CLTotalAc,y.GB_CLTotalAmt, y.GB_CLAc, y.GB_CLAmt, y.GB_CL).
I want these value to my asp.net C# page under text box (txtCLTotalAmt,
txtCL, txtGB_CLTotalAmt, txtGB_CL)
Store Procedure
ALTER proc [dbo].[Cr_CL_Report]
#ProductName text,
#BranchName text
as
begin
Select x.CLTotalAc,x.CLTotalAmt,x.CLAc, x.CLAmt,x.CL,
y.GB_CLTotalAc,y.GB_CLTotalAmt, y.GB_CLAc, y.GB_CLAmt, y.GB_CL
from
(
SELECT COUNT(dbo.DDBranchName.BCode) AS BrCode,
dbo.TblTotalCL.CLProductName AS ProductName,
SUM(dbo.TblTotalCL.CLTotalAc) AS CLTotalAc,
SUM(dbo.TblTotalCL.CLTotalAmt) AS CLTotalAmt,
SUM(dbo.TblTotalCL.CLAc) AS CLAc, SUM(dbo.TblTotalCL.CLAmt) AS CLAmt,
SUM(dbo.TblTotalCL.CLAmt)/SUM(dbo.TblTotalCL.CLTotalAmt) * 100 AS CL
FROM dbo.DDBranchName INNER JOIN
dbo.TblTotalCL ON dbo.DDBranchName.BCode = dbo.TblTotalCL.CLbrCode
WHERE (dbo.TblTotalCL.CLAsOnDate IN
(SELECT MAX(CLAsOnDate) AS Expr1 FROM dbo.TblTotalCL AS TblTotalCL_1))
AND (dbo.TblTotalCL.CLProductName LIKE #ProductName)
AND (dbo.DDBranchName.BCode LIKE #BranchName)
GROUP BY dbo.TblTotalCL.CLProductName
) x
inner join
(
SELECT COUNT(dbo.DDBranchName.BCode) AS GB_BrCode,
dbo.TblTotalCL.CLProductName AS GB_ProductName,
SUM(dbo.TblTotalCL.CLTotalAc) AS GB_CLTotalAc,
SUM(dbo.TblTotalCL.CLTotalAmt) AS GB_CLTotalAmt,
SUM(dbo.TblTotalCL.CLAc) AS GB_CLAc,
SUM(dbo.TblTotalCL.CLAmt) AS GB_CLAmt,
(SUM(dbo.TblTotalCL.CLAmt) / SUM(dbo.TblTotalCL.CLTotalAmt))*100 AS GB_CL
FROM dbo.DDBranchName INNER JOIN
dbo.TblTotalCL ON dbo.DDBranchName.BCode = dbo.TblTotalCL.CLbrCode
WHERE (dbo.TblTotalCL.CLAsOnDate IN
(SELECT MAX(CLAsOnDate) AS Expr1 FROM dbo.TblTotalCL AS TblTotalCL_1))
AND (dbo.TblTotalCL.CLProductName LIKE #ProductName)
GROUP BY dbo.TblTotalCL.CLProductName
) y on
x.ProductName = y.GB_ProductName
end
Blockquote
how can I solve this?
Getting return value from stored procedure in C#
In the link you can see how to get the values form a stored procedure. Then assign the value to the text box
Start by reading up on Ado.net. Use for instance the example here,under adonet data provider examples/sqlclient. When you have this working you have retrieved the data from the database. (imho avoid table adapters, entity framework and linqtosql if you want to grok it. instead go with manually writing for SqlCommand. or dapper to ease some of the burden.)
Now you have to get it to the web page. First decide on Webforms or Aspnetmvc. Then googlewithbing what the syntax is like.
Wash.
Rinse.
Repeat.
HTH

SQL SELECT STATEMENT IN ASP written in Frontpage

Current select statement:
SELECT *
FROM vw_svc200_open
WHERE (CUSTNMBR = '::CUSTNMBR::')
ORDER BY ::sortcolumn::
This works and all is well. But now I need to modify this select string to apply an extra filter.
SELECT *
FROM vw_svc200_open
WHERE
CASE
WHEN ::CUSTNMBR:: = 'ABC123'
THEN (CUSTNMBR = '::CUSTNMBR::' AND CNTCPRSN = '::CNTCPRSN::')
ELSE (CUSTNMBR = '::CUSTNMBR::')
END
ORDER BY ::sortcolumn::
So basically I need to have my select filter on customer number and if customer number is ABC123 then I also need it to filter on Contact person... The problem with the second SELECT (using the CASE statement) is that it throws an "error near =" on the THEN line.
The ::CUSTNMBR:: and ::CNTCPRSN:: are url string variables (What are those called again?).
Ex.
www.mywebsite.com/mypage.asp?Custnmbr=ABC123
Am I going to have to add some logic to the asp page (i.e. IF/Then) to set a variable, and then pass that variable to the *fp_sQry=* line?
I ended up using nested if statements to set the select statement.

Column Value of multiselect rows in jqgrid

Its like I have multiselect option in Jqgrid in which i want to pass the selected rows value to the server and based on the value i will delete the rows. I dont want to have the ids to do my work. For the Single row i get the cell value and delete using the same. But for multi select its not the case. In getGridParam('selarrrow'); I use this to fetch the selected rows bu the values are not getting populated. Please help me out in doing the same
When i use the following code as i saw in some sample question i can fetch the value for the single row selection but when i select multiple rows then i pass it says like "FALSE" or "UNDEFINED". What can be the issue. var grid = jQuery('#list'); var sel_id = grid.jqGrid('getGridParam', 'selarrrow'); var myCellData = grid.jqGrid('getCell', sel_id, 'CountryId');
I got this done by using the for loop for getting the selected rows id
i did some thing like this and I am able to fetch the value. Thought this might help others too please check it
var myrow;
var id = jQuery("#List").jqGrid('getGridParam','selarrrow');
if(id.length)
{
for (var i=0;i<id.length;i++) // For Multiple Delete of row
{
myrow = jQuery("#List").jqGrid('getCell',id[i],'ticker');
}
}

How to return all records for Gridview select command parameter

Using asp.net
In the gridview select command, how can I select all records, including null, for a particular field. I have a parameter which changes with the dropdown selection. I have a default list item set to % but this will not pull records with null. I tried this:
Where
((#submit LIKE '%' AND user_submit LIKE #submit OR user_submit IS NULL) OR (#submit NOT LIKE '%' AND user_submit LIKE #submit))
but am getting null records in both sides of the or statement. I aslo tried a case statement but cannot figure out how to select both % and 'is null'.
If I'm understaning correctly I think you want to select based on a criteria but also if the field has a value of null.
Something like this should work for you
select whatever from Table_1 where whatever like '%' or whatever is null

Resources