ATLAS / WEBSERVICES / C# / JAVASCRIPT / SQL SERVER

Discussion in 'ASP .Net' started by Daniel, Sep 14, 2006.

  1. Daniel

    Daniel Guest

    I wrote a pretty neat website today that can utilize no postback
    AJAXIAN type features.

    Basically to populate a drop down with a web service call that hits a
    SQL server, based off the change of another drop down.

    [.aspx file]

    <%@ Page Language="C#" Title="Call Update" AutoEventWireup="true"
    CodeFile="UpdateCall.aspx.cs" Inherits="Call_UpdateCall" %>

    <!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>Untitled Page</title>
    <link href="../Styles.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <form id="updateform" runat="server">
    <atlas:ScriptManager ID="scriptManager" EnablePartialRendering="true"
    runat="server">
    <Services><atlas:ServiceReference Path="~/Services/CallInfo.asmx"
    /></Services>
    </atlas:ScriptManager>
    <div>
    <span style="font-size: 12pt"><strong>Call Update -
    </strong></span>
    <asp:Label ID="lblCallNo" runat="server" ForeColor="#FFFF66"
    Text="#####" Font-Bold="True" Font-Italic="False"
    Font-Size="12pt"></asp:Label>&nbsp;<br />
    <br />
    Organization
    <asp:DropDownList ID="ddlOrgs" runat="server"
    DataSourceID="SqlDataSource1" DataTextField="USER_NAME"
    DataValueField="USER_NAME">
    </asp:DropDownList><br />
    <br />
    Product
    <asp:DropDownList ID="ddlProduct" runat="server">
    </asp:DropDownList><br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:SHDDBConnectionString %>"
    SelectCommand="SELECT DISTINCT USER_NAME FROM PEOPLE ORDER
    BY USER_NAME"></asp:SqlDataSource>
    </div>
    </form>

    <script type="text/javascript">

    function populateProducts()
    {
    System.Issltd.Helpdesk.CallInfo.GetOrgs("SELECT
    PRODUCT.PRODUCT_NAME FROM PEOPLE RIGHT OUTER JOIN PRODUCT ON
    PEOPLE.USER_CODE = PRODUCT.PRODUCT_USERCODE WHERE PEOPLE.USER_NAME = '"
    + document.updateform.ddlOrgs.value + "'", OnRequestComplete);
    }

    function OnRequestComplete(result)
    {
    var r;
    var resultArray = result.split("|");
    var orgs = document.updateform.ddlProduct;
    ClearList(orgs);
    for (r = 0; r < resultArray.length; r++)
    {
    orgs.options[orgs.options.length] = new
    Option(resultArray[r], resultArray[r], false, false);
    }
    }

    function ClearList(list)
    {
    for (x = list.length; x >= 0; x--)
    {
    list[x] = null;
    }
    }



    </script>
    </body>
    </html>

    [.aspx.cs]

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class Call_UpdateCall : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    lblCallNo.Text = Request.QueryString["inp_call_id"];
    ddlOrgs.Attributes.Add("onChange", "populateProducts()");
    }

    }

    [.asmx]

    <%@ WebService Language="C#" Class="System.Issltd.Helpdesk.CallInfo" %>

    using System.Configuration;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Xml;
    using System.Data.SqlClient;
    using System.Data;
    using System;

    namespace System.Issltd.Helpdesk
    {

    [WebService]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class CallInfo : System.Web.Services.WebService
    {

    [WebMethod]
    public String GetOrgs(String query)
    {
    string errorMessage = "";
    string dataConcat = "";
    //Connection string is stored
    //in the web.config file as an appSetting
    string connectionString =
    ConfigurationManager.ConnectionStrings["SHDDBConnectionString"].ConnectionString;
    SqlConnection dbConnection = null;
    // Open a connection to the database
    try
    {
    dbConnection = new SqlConnection(connectionString);
    dbConnection.Open();
    }
    catch (Exception ex)
    {
    errorMessage = ex.Message;
    }
    if (errorMessage == "")
    {
    // Build an insert command
    string SQL = query;
    SqlCommand SqlCmd = new SqlCommand(SQL, dbConnection);

    try
    {
    SqlDataAdapter SqlDA = new SqlDataAdapter();
    SqlDA.SelectCommand = SqlCmd;
    DataSet ds = new DataSet();
    SqlDA.Fill(ds);
    foreach (DataRow dataRow in ds.Tables[0].Rows)
    {
    dataConcat = dataConcat + "|" +
    dataRow[0].ToString();
    }
    dataConcat = dataConcat.Substring(1);
    dbConnection.Close();

    }
    catch (System.Exception ex)
    {
    errorMessage = ex.Message;

    }
    finally
    {
    dbConnection.Dispose();
    }
    }
    return dataConcat;
    }
    }
    }


    Let me know if you have any questions... Now I'm trying to reuse the
    javascript code for many controls. Let's see what happens.
     
    Daniel, Sep 14, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmV6YQ==?=
    Replies:
    3
    Views:
    18,021
    Carlos Barini
    Jun 7, 2004
  2. Daves
    Replies:
    1
    Views:
    396
  3. Geoff
    Replies:
    3
    Views:
    467
    Patrick.O.Ige
    Jan 16, 2006
  4. Replies:
    2
    Views:
    1,709
  5. Replies:
    4
    Views:
    630
Loading...

Share This Page