By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,537 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,537 IT Pros & Developers. It's quick & easy.

ATLAS / WEBSERVICES / C# / JAVASCRIPT / SQL SERVER

P: n/a
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.

Sep 14 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.