How can I accomplish something like this? I want to use the event of a textbox (TextChanged event) to automatically fetch data from a DB hence I use something like this
protected void StaffID_TextChanged(object sender, EventArgs e)
{
//fetch information from DB
}
StaffID.Text is the name of a control on my application so what I want to do is, when I so something like this
https://localhost/signatureapplication.aspx?StaffID=9655096 once it transfers 9655096 to the textbox, it automatically uses the value of 9655096 in that textbox to search and fetch information from the DB does not work. Is there something I am missing?
I have set the Textbox value to use AutoPostBack="True" like this
<asp:TextBox ID="StaffID" runat="server" AutoPostBack="True" OnTextChanged="StaffID_TextChanged"></asp:TextBox>
Then added a reference for C# forms to see if it would fire a messagebox and then call it like so
protected void StaffID_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Hello from me!");
}
Let me show what I have done so far.
I do this to get the staff ID as a URL parameter
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="signaturestart.aspx.cs" Inherits="MYSignatureAccess.signaturestart" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
StaffID.Text = Convert.ToString(Request.QueryString["StaffID"]);
StaffID.DataBind();
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="Web Page Maker">
<style type="text/css">
/*----------Text Styles----------*/
.ws6 {font-size: 8px;}
.ws7 {font-size: 9.3px;}
.ws8 {font-size: 11px;}
.ws9 {font-size: 12px;}
.ws10 {font-size: 13px;}
.ws11 {font-size: 15px;}
.ws12 {font-size: 16px;}
.ws14 {font-size: 19px;}
.ws16 {font-size: 21px;}
.ws18 {font-size: 24px;}
.ws20 {font-size: 27px;}
.ws22 {font-size: 29px;}
.ws24 {font-size: 32px;}
.ws26 {font-size: 35px;}
.ws28 {font-size: 37px;}
.ws36 {font-size: 48px;}
.ws48 {font-size: 64px;}
.ws72 {font-size: 96px;}
.wpmd {font-size: 13px;font-family: Arial,Helvetica,Sans-Serif;font-style: normal;font-weight: normal;}
/*----------Para Styles----------*/
DIV,UL,OL /* Left */
{
margin-top: 0px;
margin-bottom: 0px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="image1" style="position:absolute; overflow:hidden; left:148px; top:58px; width:113px; height:70px; z-index:0"><img src="images/Image.png" alt="" title="" border=0 width=113 height=70></div>
<div id="roundrect1" style="position:absolute; overflow:hidden; left:140px; top:151px; width:177px; height:35px; z-index:1"><img border=0 width="100%" height="100%" alt="" src="images/roundrect100678000.gif"></div>
<div id="text1" style="position:absolute; overflow:hidden; left:157px; top:157px; width:150px; height:19px; z-index:2">
<div class="wpmd">
<div><font color="#FFFFFF" face="Verdana"><B>Signing Document</B></font></div>
</div></div>
<div id="hr1" style="position:absolute; overflow:hidden; left:150px; top:177px; width:933px; height:17px; z-index:3">
<hr size=1 width=933 color="#970097">
</div>
<div id="hr2" style="position:absolute; overflow:hidden; left:173px; top:473px; width:933px; height:17px; z-index:4">
<hr size=1 width=933 color="#970097">
</div>
<div id="text2" style="position:absolute; overflow:hidden; left:174px; top:486px; width:150px; height:23px; z-index:5">
<div class="wpmd">
<div><font class="ws8">©</font><font class="ws8"> 2019 WEMA Bank Nigeria</font></div>
</div></div>
<div id="table1" style="position:absolute; overflow:hidden; left:165px; top:511px; width:952px; height:48px; z-index:6">
<div class="wpmd">
<div><TABLE bgcolor="#FFFFFF" border=0 bordercolorlight="#C0C0C0" bordercolordark="#808080">
<TR valign=top>
<TD width=983 height=61><BR>
</TD>
</TR>
</TABLE>
</div>
</div></div>
<div id="image2" style="position:absolute; overflow:hidden; left:505px; top:231px; width:285px; height:190px; z-index:7"><img src="images/unnamed.gif" alt="" title="" border=0 width=285 height=190></div>
<asp:TextBox ID="StaffID" runat="server" AutoPostBack="True" OnTextChanged="StaffID_TextChanged1"></asp:TextBox>
</body>
</form>
</html>
Now I want to use the code behind to do the search like this:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
namespace WEMASignatureAccess
{
public partial class signaturestart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void SignDocument()
{
string StaffID = Request.QueryString["staffID"];
//string StaffID = "208045P";
string constring = @"Data Source=DESKTOP-9CM4N5S\SQLEXPRESS;Initial Catalog=SignatureBox2;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
string conQuery = "select * from SignatureBox_DB where StaffID = @StaffID";
using (SqlCommand cmd = new SqlCommand(conQuery, con))
{
cmd.Parameters.AddWithValue("@StaffID", StaffID);
using (SqlDataReader rd = cmd.ExecuteReader())
{
try
{
if (rd.Read())
{
string filePath = "~/ImagesSignature/";
string myFile = "sinature.jpg";
string fileName = Path.Combine(filePath, myFile);
byte[] imageBytes = Convert.FromBase64String(rd["SignatureBase64"].ToString());
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
File.WriteAllBytes(Server.MapPath(fileName), imageBytes);
string myphysicalPath = System.Web.HttpContext.Current.Server.MapPath(fileName);
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = null;
doc = app.Documents.Open(@"C:\Users\emi\Desktop\Jasmine.docx", Type.Missing);
var Signature2Ctrl = doc.SelectContentControlsByTag("Jasmine");
var testingCtrl = Signature2Ctrl[1];
testingCtrl.Range.InlineShapes.AddPicture(myphysicalPath, Type.Missing, Type.Missing, Type.Missing);
doc.Save();
MessageBox.Show("Document Signed successfully!");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
Response.Redirect("signaturecomplete.aspx");
}
}
}
}
protected void StaffID_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Hello from me!");
}
protected void StaffID_TextChanged1(object sender, EventArgs e)
{
}
}
}