I am trying to run some code in my Code Behind to query my SQL Server (using a JavaScript variable) and then pass the result as an Integer back to my Javascript.
I have attempted to do this, by initially running some Javascript to get a variable for the SQL SELECT statement, then passing this to a hidden ASP field (currently a textbox for test purposes).
However everytime I run the code, refreshHTML(), the hidden field (textbox) value is blank and it returns the value 999999999.
When I run the application, I can see the textbox is populated with the correct value.
Here is my Code Behind in C#
public int ucount
{
get
{
if (String.IsNullOrEmpty(invoicenumberhidden.Text))
{
return 999999999;
}
else {
int invoicenumber = int.Parse(invoicenumberhidden.Text);
string commandText = "SELECT COUNT(*) AS distinct_count FROM (SELECT DISTINCT [inv_section],[invoice_number], [cust_po] FROM [Indigo].[dbo].[invoice_items] WHERE invoice_number=" + invoicenumber + ") AS I;";
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Indigo2.Properties.Settings.Constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(commandText, conn))
{
conn.Open();
cmd.ExecuteNonQuery();
int uniquecount = (Int32)cmd.ExecuteScalar();
conn.Close();
return uniquecount;
}
}
}
}
Here is my JavaScript which is triggered on an ASP OnClientClick event function refreshHtml()
//Get Primary Key from Grid1
var grid = jQuery("#<%= JQGrid1.ClientID %>");
var rowKey = grid.getGridParam("selrow");
document.getElementById('<%= invoicenumberhidden.ClientID %>').value = rowKey;
var jsucount = '<%=this.ucount%>';
alert(jsucount);
This is my relevant ASP code
<div hidden> <asp:Button ID="btnDownload" runat="server" OnClientClick="refreshHtml();" OnClick="btnDownloadButton_Click" Text="Export PDF"></asp:Button></div>
<asp:TextBox ID="invoicenumberhidden" runat="server"></asp:TextBox>