当我点击按钮时,我正试图从数据库中获取数据。 我有一个下拉列表,一个文本框和一个按钮。 当我点击按钮时,它应该从下拉列表中获取输入值并从数据库中返回一个值并将其放入文本框中。
下拉列表有帐号。
这是我的ajax代码,它在head标签内部
<script type="text/javascript">
$(document).ready(function()
{
alert("Entering");
$('#btnBalance').click(function(){
var accountNo = $('#DropDownListFromWhichAccount').val();
$.ajax({
url:'BalanceService.asmx/getBalance',
data:{acc:accountNo},
method:'post',
dataType: 'xml',
success:function(data)
{
console.log(data);
var jqueryXml=$(data);
$('#txtBalance').val(jqueryXml.find('balance').text())
//$('#txtBalance').val("ok")
alert(jqueryXml.find('balance').text());
},
error:function(err)
{
alert(err);
}
})
})
});
</script>
html代码:
<div class="row content-padding-top">
<div class="col-lg-3">
<label for="DropDownListFromWhichAccount" class="content-label control-label">Debit (From)</label>
</div>
<div class="col-lg-3">
<asp:DropDownList ID="DropDownListFromWhichAccount" runat="server" class="form-control text-box-border" >
<asp:ListItem>Cash</asp:ListItem>
</asp:DropDownList>
</div>
<div class="col-lg-2">
<asp:TextBox ID="txtBalance" runat="server" class="form-control text-box-border" placeholder="Balance" ></asp:TextBox>
</div>
<div class="col-lg-1">
<asp:Button ID="btnBalance" type="button" runat="server" class="btn btn-default" Text="Check" />
</div>
</div>
asmx文件:
namespace NibrassProject
{
/// <summary>
/// Summary description for BalanceService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class BalanceService : System.Web.Services.WebService
{
[WebMethod]
public BalanceChecking getBalance(string accountNo)
{
BalanceChecking bc = new BalanceChecking();
bc.balance=bc.checkBalance(accountNo);
bc.balance = "faisal";
return bc;
}
}
}
BalanceChecking.cs文件
namespace NibrassProject
{
public class BalanceChecking
{
public string balance { get; set; }
string cs = ConfigurationManager.ConnectionStrings["Nibrass_DBConnectionString"].ConnectionString;
public string checkBalance(string acc)
{
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT Balance FROM Account_Balance_tb WHERE Account_Card_Number=" + acc + " ORDER BY Id DESC";
// cmd.Parameters.AddWithValue("@Account_Number",accountNo);
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
balance = rd["Balance"].ToString();
break;
}
rd.Close();
}
return balance;
}
}
错误讯息: 未捕获的异常:内存不足