Tuesday, July 12, 2016

Computed Columns in a Table


How to create computed "Age" column


  • Create the table like this.

  • Enter the column name and accept the default data type (nchar(10)). The Database Engine determines the data type of the computed column by applying the rules of data type precedence to the expressions specified in the formula.
  • In the Column Properties tab, expand the Computed Column Specification property.
  • In the (Formula) child property, enter the expression for this column in the grid cell to the right. [code] (0 + Convert(Char(8),GETDATE(),112) - Convert(Char(8),DOB,112)) / 10000.
  • [/code]

Friday, July 8, 2016

Dynamically bind data to an rdlc report(Part1)


From this post I'm going to describe how to bind dynamic data to an rdlc report. In here it loads data to the report based on selected value of a drop down. That means we can display different reports based on given parameters.


  • First of all we will do the design of the web page (with a dropdown,a button and a reportviewer) 



  • Then add code behind methods as below.

[code] protected void btnview_Click(object sender, EventArgs e) { string id = ddlId.SelectedValue; string filename = string.Empty; string[] queries = null; if (id == "1") { filename = "Test7-1"; string query1 = "select id,lat from Test3 where id=" + id + ""; string query2 = "select lng,colorId from Test3 where id=" + id + ""; queries = new string[2]; queries[0] = query1; queries[1] = query2; } else if (id == "2") { filename = "Test7-2"; string query1 = "select id,lat from Test3 where id=" + id + ""; string query2 = "select lng,colorId from Test3 where id=" + id + ""; queries = new string[2]; queries[0] = query1; queries[1] = query2; } runRptViewer(filename, queries); ReportViewer1.Visible = true; } private void runRptViewer(string fileName,string[] queries) { DataTable[] datatables = fillDataTables(queries); string reportpath = @"D:\Madara\Reports\" + fileName + ".rdl"; this.ReportViewer1.Reset(); this.ReportViewer1.LocalReport.ReportPath = reportpath; getDataSetNames(datatables); } private DataTable[] fillDataTables(string[] queries) { int queryCount = queries.Length; DataTable[] datatables = new DataTable[queryCount]; for (int i = 0; i < queryCount; i++) { DataTable dt = page6Service.getData(queries[i]); datatables[i] = dt; } return datatables; } private void getDataSetNames(DataTable[] datatables) { ReportDataSourceCollection dataSources = ReportViewer1.LocalReport.DataSources; this.ReportViewer1.LocalReport.DataSources.Clear(); IList dataSetNames; dataSetNames = this.ReportViewer1.LocalReport.GetDataSourceNames(); for (int i = 0; i < dataSetNames.Count; i++) { dataSources.Add(new ReportDataSource(dataSetNames[i], datatables[i])); } this.ReportViewer1.DataBind(); this.ReportViewer1.LocalReport.Refresh(); } [/code]

  • Values of the sql data table.

  • Result will be like this.




Prevent button double click


Add this in page load.

btnGenerateInvoice.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnGenerateInvoice, null) + ";");

Create a report (RDLC) to show one record per page



  • Bind the data set to the report.


  • Suppose you want to group the data based on Ref No.
  • Add a List control.
  • Drag and drop any attribute from data set inside to that List control.
  • Right click on Details, under Row Groups and select Group Properties.
  • Under General tab, click add and select "Ref No" from Group on.


  • Under Page Breaks tab, select the page break options as follows.

  • Then you can design your report as you want.