Showing posts with label rad controls. Show all posts
Showing posts with label rad controls. Show all posts

Tuesday, June 17, 2014

Fetch value from SQL DataSource and use select command to fetch individual row details.

DataView dv = (DataView)SqlDataSource.Select(DataSourceSelectArguments.Empty);
int reorderedProducts = (int)dv.Table.Rows[0][2];

DataRow[] dr = dv.Table.Select("Code='" + val + "'");
string id = dr[0]["Id"].ToString();


Code to fetch Data from database using SQL DataAdaptor and fill DataSet

if (con.State == ConnectionState.Closed)
        {
            con.Open();   // check connection and if closed then open it.
        }
SqlCommand cmd = new SqlCommand();
cmd.Connection = con; //Connection object
cmd.CommandText = "Stored Procedure Name";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Id", SqlDbType.NVarChar).Value = Convert.ToInt32(id); //Parameter
SqlDataAdapter adt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adt.Fill(ds);


Code to save data into database

if (con.State == ConnectionState.Closed)
        {
            con.Open();   // check connection and if closed then open it.
        }
SqlCommand cmd = new SqlCommand(); // Declare Sqlcommand Function
cmd.Connection = con;
cmd.CommandText = "Stored Procedure Name";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.NVarChar).Value = txtbox.Text;
cmd.Parameters.Add("@ErrorStatus", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
if (Convert.ToInt32(cmd.Parameters["@ErrorStatus"].Value) == 1)
   {
      reset();
      DisplayAJAXMessage(this, "Inserted Successfully");
   }

To display message in popup

static public void DisplayAJAXMessage(Control page, string msg)
    {
        string myScript = String.Format("alert('{0}');", msg);
        ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
    }

Telerik Radgridview methods and functions summary:

Telerik Radgridview Operations: (Here grid name is radgrid)

1. Fetch Specific row details by clicking on link button in the row:

((sender as LinkButton).Parent.Parent as GridItem).Selected = true;
String ID = ((radgrid.SelectedItems[0].FindControl("lblID") as Label).Text).ToString();
 OR
HiddenField hdnId = radgrid.SelectedRow.FindControl("hdnId") as HiddenField;

2. Code to change pages in radgrid using page index changed event:

 protected void radgrid_PageIndexChanged(object sender, Telerik.Web.UI.GridPageChangedEventArgs e)
    {
        radgrid.DataSource = (DataSet)Session["data"];
        radgrid.DataBind();
    }

3. Code to change the grid page size to be displayed using page size changed event:

protected void radgrid_PageSizeChanged(object sender, Telerik.Web.UI.GridPageSizeChangedEventArgs e)
    {
        radgrid.DataSource = (DataSet)Session["data"];
        radgrid.DataBind();
    }

4. Code to enable filter option with the help of item command event:

protected void radgrid_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Filter")
        {
            radgrid.DataSource = (DataSet)Session["data"];
            radgrid.DataBind();
        }
    }

5. Code to export radgridview data to excel file:

 protected void btnExcel_Click(object sender, EventArgs e)
    {
        radgrid.ExportSettings.ExportOnlyData = true;
        radgrid.ExportSettings.IgnorePaging = true;
        radgrid.DataSource = (DataSet)Session["data"];
        radgrid.DataBind();
        radgrid.ExportSettings.OpenInNewWindow = true;
        radgrid.ExportSettings.FileName = "FileName";
        if (radgrid.Items.Count > 0)
        {
            radgrid.MasterTableView.ExportToExcel();
        }
    }

6. Design code to add column in the radgridview:



 <%# Container.DataItemIndex +1 %>
 




7. Design code to Make a GridView Column Visible using fetched value:


 
        LinkButton_Click" Text="Print" Visible='<%# (bool.Parse(Eval("IsVisible").ToString())) %>' ToolTip='<%# Eval("Id") %>'>




Wednesday, November 28, 2012

Display a file in NewPage and export a file to Excel.

Display a file on Link Button Click.


protected void linkbtnFile_Click(object sender, EventArgs e)
    {
        string link =  Server.MapPath(".") + "\\\\" +;
        Response.ContentType = "application/"+linkbtnFile.Text.Substring(linkbtnFile.Text.IndexOf('.')+1);
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + link);
        Response.TransmitFile(link);
        Response.End();
    }


Export a File from Radgrid to Excel file.

protected void btnExport_Click(object sender, EventArgs e)
    {
        RadGrid1.MasterTableView.ExportToExcel();
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
    }

Sunday, October 14, 2012

Display Report Using Report Viewer on Button Click using C# in asp.net

To Display the Report on Microsoft Report Viewer following are the steps:

Step 1 : Add the Microsoft Report Viewer from Reporting tab of the Tool Box.

Fig : To Add Report Viewer in the Design Page.

 Step 2: On "Show" Button Click display the report.

On button click we provide the Report Viewer parameters to display the appropriate report with respect to the data provided using the dropdownlist.

Fig 2: Report Display
In the above figure Fig 2, the user selects the details from the dropdownlist and is passed as parameter on "Show" button click.

Step 3: Code to be written on Show Button Click event.

 protected void btnShow_Click(object sender, EventArgs e)
    {
        try
        {
            ReportParameter[] parm = new ReportParameter[2];
            parm[0] = new ReportParameter("<BatchYear>", ddlBatch.SelectedValue);
            parm[1] = new ReportParameter("<ProgramCode>", ddlProgramName.SelectedValue);
            ReportViewer1.ShowCredentialPrompts = false;
            ReportViewer1.ShowParameterPrompts = false;
            ReportViewer1.ServerReport.ReportServerCredentials = new ReportCredentials(UserId, Password, DomainName);
            ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
            ReportViewer1.ServerReport.ReportServerUrl = new System.Uri(ReportServerUrl);
            ReportViewer1.ServerReport.ReportPath = "//<ReportPath>"; 
            ReportViewer1.ServerReport.SetParameters(parm);
            ReportViewer1.ServerReport.Refresh();
        }
        catch (Exception ex)
        {
            DisplayAJAXMessage(this,"Error:: "+ex);
        }
    }


static public void DisplayAJAXMessage(Control page, string msg)
    {
        string myScript = String.Format("alert('{0}');", msg);
        ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
    }


Note: The text shown in bold need to added as per requirement

View Amit Lal's profile on LinkedIn
Showing posts with label rad controls. Show all posts
Showing posts with label rad controls. Show all posts

Tuesday, June 17, 2014

Fetch value from SQL DataSource and use select command to fetch individual row details.

DataView dv = (DataView)SqlDataSource.Select(DataSourceSelectArguments.Empty);
int reorderedProducts = (int)dv.Table.Rows[0][2];

DataRow[] dr = dv.Table.Select("Code='" + val + "'");
string id = dr[0]["Id"].ToString();


Code to fetch Data from database using SQL DataAdaptor and fill DataSet

if (con.State == ConnectionState.Closed)
        {
            con.Open();   // check connection and if closed then open it.
        }
SqlCommand cmd = new SqlCommand();
cmd.Connection = con; //Connection object
cmd.CommandText = "Stored Procedure Name";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Id", SqlDbType.NVarChar).Value = Convert.ToInt32(id); //Parameter
SqlDataAdapter adt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adt.Fill(ds);


Code to save data into database

if (con.State == ConnectionState.Closed)
        {
            con.Open();   // check connection and if closed then open it.
        }
SqlCommand cmd = new SqlCommand(); // Declare Sqlcommand Function
cmd.Connection = con;
cmd.CommandText = "Stored Procedure Name";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.NVarChar).Value = txtbox.Text;
cmd.Parameters.Add("@ErrorStatus", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
if (Convert.ToInt32(cmd.Parameters["@ErrorStatus"].Value) == 1)
   {
      reset();
      DisplayAJAXMessage(this, "Inserted Successfully");
   }

To display message in popup

static public void DisplayAJAXMessage(Control page, string msg)
    {
        string myScript = String.Format("alert('{0}');", msg);
        ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
    }

Telerik Radgridview methods and functions summary:

Telerik Radgridview Operations: (Here grid name is radgrid)

1. Fetch Specific row details by clicking on link button in the row:

((sender as LinkButton).Parent.Parent as GridItem).Selected = true;
String ID = ((radgrid.SelectedItems[0].FindControl("lblID") as Label).Text).ToString();
 OR
HiddenField hdnId = radgrid.SelectedRow.FindControl("hdnId") as HiddenField;

2. Code to change pages in radgrid using page index changed event:

 protected void radgrid_PageIndexChanged(object sender, Telerik.Web.UI.GridPageChangedEventArgs e)
    {
        radgrid.DataSource = (DataSet)Session["data"];
        radgrid.DataBind();
    }

3. Code to change the grid page size to be displayed using page size changed event:

protected void radgrid_PageSizeChanged(object sender, Telerik.Web.UI.GridPageSizeChangedEventArgs e)
    {
        radgrid.DataSource = (DataSet)Session["data"];
        radgrid.DataBind();
    }

4. Code to enable filter option with the help of item command event:

protected void radgrid_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Filter")
        {
            radgrid.DataSource = (DataSet)Session["data"];
            radgrid.DataBind();
        }
    }

5. Code to export radgridview data to excel file:

 protected void btnExcel_Click(object sender, EventArgs e)
    {
        radgrid.ExportSettings.ExportOnlyData = true;
        radgrid.ExportSettings.IgnorePaging = true;
        radgrid.DataSource = (DataSet)Session["data"];
        radgrid.DataBind();
        radgrid.ExportSettings.OpenInNewWindow = true;
        radgrid.ExportSettings.FileName = "FileName";
        if (radgrid.Items.Count > 0)
        {
            radgrid.MasterTableView.ExportToExcel();
        }
    }

6. Design code to add column in the radgridview:



 <%# Container.DataItemIndex +1 %>
 




7. Design code to Make a GridView Column Visible using fetched value:


 
        LinkButton_Click" Text="Print" Visible='<%# (bool.Parse(Eval("IsVisible").ToString())) %>' ToolTip='<%# Eval("Id") %>'>




Wednesday, November 28, 2012

Display a file in NewPage and export a file to Excel.

Display a file on Link Button Click.


protected void linkbtnFile_Click(object sender, EventArgs e)
    {
        string link =  Server.MapPath(".") + "\\\\" +;
        Response.ContentType = "application/"+linkbtnFile.Text.Substring(linkbtnFile.Text.IndexOf('.')+1);
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + link);
        Response.TransmitFile(link);
        Response.End();
    }


Export a File from Radgrid to Excel file.

protected void btnExport_Click(object sender, EventArgs e)
    {
        RadGrid1.MasterTableView.ExportToExcel();
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
    }

Sunday, October 14, 2012

Display Report Using Report Viewer on Button Click using C# in asp.net

To Display the Report on Microsoft Report Viewer following are the steps:

Step 1 : Add the Microsoft Report Viewer from Reporting tab of the Tool Box.

Fig : To Add Report Viewer in the Design Page.

 Step 2: On "Show" Button Click display the report.

On button click we provide the Report Viewer parameters to display the appropriate report with respect to the data provided using the dropdownlist.

Fig 2: Report Display
In the above figure Fig 2, the user selects the details from the dropdownlist and is passed as parameter on "Show" button click.

Step 3: Code to be written on Show Button Click event.

 protected void btnShow_Click(object sender, EventArgs e)
    {
        try
        {
            ReportParameter[] parm = new ReportParameter[2];
            parm[0] = new ReportParameter("<BatchYear>", ddlBatch.SelectedValue);
            parm[1] = new ReportParameter("<ProgramCode>", ddlProgramName.SelectedValue);
            ReportViewer1.ShowCredentialPrompts = false;
            ReportViewer1.ShowParameterPrompts = false;
            ReportViewer1.ServerReport.ReportServerCredentials = new ReportCredentials(UserId, Password, DomainName);
            ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
            ReportViewer1.ServerReport.ReportServerUrl = new System.Uri(ReportServerUrl);
            ReportViewer1.ServerReport.ReportPath = "//<ReportPath>"; 
            ReportViewer1.ServerReport.SetParameters(parm);
            ReportViewer1.ServerReport.Refresh();
        }
        catch (Exception ex)
        {
            DisplayAJAXMessage(this,"Error:: "+ex);
        }
    }


static public void DisplayAJAXMessage(Control page, string msg)
    {
        string myScript = String.Format("alert('{0}');", msg);
        ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
    }


Note: The text shown in bold need to added as per requirement

View Amit Lal's profile on LinkedIn