Tech Junkie Blog - Real World Tutorials, Happy Coding!: 2013

Tuesday, October 8, 2013

If you receive the WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery' error, put the following lines of code in the Appliction_Start method in the Global.asax.cs file

        protected void Application_Start(object sender, EventArgs e)
        {
            string jqueryVersion = "1.8.0";

            System.Web.UI.ScriptManager.ScriptResourceMapping.AddDefinition("jquery", 
                                                new System.Web.UI.ScriptResourceDefinition
            {
                Path = "~/Scripts/jquery-" + jqueryVersion + ".min.js",
                DebugPath = "~/Scripts/jquery-" + jqueryVersion + ".js",
                CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery" + jqueryVersion + ".min.js",
                CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery" + jqueryVersion + ".js",
                CdnSupportsSecureConnection = true,
                LoadSuccessExpression = "Window.jquery"
                
            });
        }


The above lines of code registers the jQuery library with the ScripManager. This will get rid of the error, you can do the same this with jQueryUI library.


Friday, September 27, 2013

Thursday, September 19, 2013

The reason the $find method is not working for you is because the RadGrid loads on late binding. You need to get the RadGrid client object in the pageLoad() function like in the code below. After you get it you can set the global variables in the your Javascript so the other function will have access to the RadGrid object.

    <telerik:RadCodeBlock ID="RadCodeBlock2" runat="server">
        <script type="text/javascript">
            var radGrid = null;
            var masterTableView = null;
            var rgDataItems = null;



            function pageLoad() {
                radGrid = $find("<%= RadGrid1.ClientID %>");
                masterTableView = grid.get_masterTableView();
                rgDataItems = masterTable.get_dataItems();
             }
        </script>
    </telerik:RadCodeBlock>

Tuesday, September 17, 2013

In this blog, I will go over how you can simulate a button click postback.  By using the RaisePostBackEvent()  method.  Many of you probably want to do this because you wanted to refresh your GridView by faking a postback.  As you will see the two methods presented on this blog does not perform an actual postback, even though it behaves like it does.


Mark Up

    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    
    </div>
    </form>


Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.RaiseEvent(this, new EventArgs());
            if (!Page.IsPostBack)
            {
                this.Button1_Click(this, new EventArgs());
            }
        }

        protected void RaiseEvent(object sender, EventArgs e)
        {
            this.RaisePostBackEvent(Button1, " ");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write("You've clicked " + Button1.Text + " ");
            
            if (Page.IsPostBack)
            {
                Response.Write("this is a post back");
            }
            else if (!Page.IsPostBack)
            {
                Response.Write("this is not a post back");
                Response.Write("<br/>");
            }
        }
    }
}


After you run the code you will find out that both ways of doing this does not cause a post back event.



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" EnableEventValidation="false" Inherits="WebApplication2._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js"></script>
</head>
    <script type="text/javascript">
        
        $(document).ready(function () {
            $("#Button1").click();

        });
    </script>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>


Thursday, September 5, 2013

In this blog we will go over how to query an Oracle database in ASP.NET using the System.Data.OracleClient data provider in C#.

Namespaces:

using System.Web.Configuration;
using System.Data.OracleClient;
using System.Data;

            string cs = WebConfigurationManager.ConnectionStrings["SomeConnectionString"].ConnectionString;

            using (OracleConnection oc = new OracleConnection(cs))
            {
                oc.Open();
                DataTable dt = new DataTable(); 
                OracleCommand ocmd = new OracleCommand("SELECT * FROM SOMETABLE", oc);
                OracleDataAdapter oda = new OracleDataAdapter(ocmd);

                oda.Fill(dt);

                GridView1.DataSource = dt;
                GridView1.DataBind();

            }
Previous: Oracle Date Format And Compare

Wednesday, September 4, 2013

Oracle dates have a different format than SQL Server dates. So to select a date for the Oracle database you have to have the date in the following format.
string myDate = "10/9/2012 2:55:25 PM";

  string sql = "SELET * FROM SomeTable WHERE SomeDateField=" +
       "to_date('" + myDate + "','" + "MM/DD/YYYY HH:MI:SS " + 
       myDate.Substring(myDate.Length - 2) + "');";

Next: C# Querying From An Oracle Database

Tuesday, September 3, 2013














RadAjaxManager ram = RadAjaxManager.GetCurrent(this.Page);

Tuesday, August 13, 2013














1. Download the Toolkit at

http://ajaxcontroltoolkit.codeplex.com/

3. Extract the download

4. Add reference to AjaxControlToolKit.DLL

5. Register the DLL on your aspx page

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html>


6. Add ToolkitScriptManager

    <form id="form1" runat="server">
    <div>
    <asp:ToolkitScriptManager ID="ToolKitManager1" runat="server"></asp:ToolkitScriptManager>


Wednesday, July 17, 2013

A lot of times you want to call a stored procedure quickly in the code to access the stored procedure.  You don't want to go through the trouble of dragging an SqlDataSource control in the design view just to use the stored procedure.

Here is how you would call the "Top Ten Most Expensive Products" stored procedure in the Northwind database.

1.  First you need the namespaces

using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;

2. Then get the Northwind connection string value from the Web.config file
string connectString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].
ConnectionString;

3. Now call the stored procedure and output the result from the SqlDataReader
  using (SqlConnection conn = new SqlConnection(connectString))
  {    
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "GetProductsAvgPrice";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;
            Response.Write("Products Average Price is $" + (decimal)cmd.ExecuteScalar());
    }

In the above code the ExecuteScalar() method executes a call to the stored procedure called "GetProductsAvgPrice" which uses the aggregate function AVG() to get the average products price. The only thing you have to watch out for is that you have to convert the type to the appropriate type if you want to use it. Since currency is of type decimal you want to use the result into a decimal value because the ExecuteScalar() method returns an object type.


Search This Blog