Wednesday, July 11, 2012

SQL Select result displays Horizontally Separate by comma

Let say you have a table structure and data as follow:


Id     Grade
1       3
1       9


then you would need a SQL select output as


Id   Grade
1     3, 9


then do this:

DECLARE @grade_list VARCHAR(MAX

SELECT @ grade_list  = CASE WHEN @ grade_list  IS NULL THEN CONVERT(VARCHAR,Grade) 
ELSE @ grade_list  + ', ' + CONVERT(VARCHAR,Grade) END
FROM Table1where Id = 1
SELECT @grade_list as Grade


or you could do like this:

SELECT Id,
 SUBSTRING
 (
  SELECT  (', ' + Grade)
  FROM table1 t2 
  WHERE t1.Id = t2.Id
  ORDER BY t1.Id, t2.Id
  FOR XML PATH('')), 3, 1000)
FROM table1 t1
GROUP BY Id


Happy Coding!!

Wednesday, March 28, 2012

How to force a pdf file to download ASP.NET

If you try to open pdf files on a browser, those are automatically open but if you need to force the browser to directly download the pdf instead you have to add file header and give the full path. see the example:


<span style="color:red">Click <a href="download.aspx"
target="_blank"><u> here </u></a> to download resume.</span>



then on the page load method of the download.aspx page, write this:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=documents/resume.pdf")
Response.ContentType = "application/pdf"      Response.WriteFile(Server.MapPath("~/documents/resume.pdf"))
Response.[End]()
End Sub


Happy Coding!!

Wednesday, February 22, 2012

Read & Display SQL data into Label

Code:

Dim conn As New SqlConnection
(ConfigurationManager.ConnectionStrings("SqlServerExecSP").ToString())
conn.Open()
Dim sql As String = "Select Name from People where Id = 1"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
Dim rd As SqlDataReader = cmd.ExecuteReader()

While rd.Read()
label1.Text = rd("Name")
End While


Change the connection string and table name in the above code.

Happy Programming!!

Checkbox-- Select at least one box using jQuery

Code:
<script>
$(document).ready(function () {
$('.error').hide();
$('.submit').click(function (event) {
var count = $('input:checked').length;
if (count == 0) {
alert("select at least one");
return false;
}
else

{
return true;
}
});
});
</script>

<input id="app" class="box" value="5" type="checkbox" name="apple">Apple$5

<input id="ora" class="box" value="2" type="checkbox" name="orange">Orange $2

<input id="mel" class="box" value="1" type="checkbox" name="melon">Melon$1

<input id="gra" class="box" value="3" type="checkbox" name="grape">Grapes $3

<input class="submit" value="Submit" type="submit">


more:
http://code.google.com/p/m-jq-projects/wiki/enableControlOnCheck_jQuery