字符串引用未设置为字符串实例

时间:2019-01-18 11:54:23

标签: c# asp.net topaz-signatures

我正在使用此Topaz签名板,并尝试将其保存到数据库中。现在,我有一个输入字段,可以在文本框(HTML)中获取用户名,如下所示:

<input type="text" name="username" id="username" />

现在,当我像这样在后面的代码中调用它时

protected void Page_Load(object sender, EventArgs e)
{
    //Put user code to initialize the page here
    Topaz.SigPlusNET sigObj = new Topaz.SigPlusNET();
    sigObj.AutoKeyStart();

    //use the same data to decrypt signature
    sigObj.SetAutoKeyData(Request.Form["username"]); <--- Error shows here

    sigObj.AutoKeyFinish();
    sigObj.SetSigCompressionMode(1);
    sigObj.SetEncryptionMode(2);

    //Now, get sigstring from client
    //Sigstring can be stored in a database if 
    //a biometric signature is desired rather than an image
    sigObj.SetSigString(Request.Form["hidden"]);
    if (sigObj.NumberOfTabletPoints() > 0)
    {
        sigObj.SetImageFileFormat(0);
        sigObj.SetImageXSize(500);
        sigObj.SetImageYSize(150);
        sigObj.SetImagePenWidth(8);
        sigObj.SetJustifyX(5);
        sigObj.SetJustifyY(5);
        sigObj.SetJustifyMode(5);
        System.Drawing.Image img = sigObj.GetSigImage();
        base64ImageString = ImageToBase64(img, System.Drawing.Imaging.ImageFormat.Jpeg);
        /* String path;
         //path = System.AppDomain.CurrentDomain.BaseDirectory & "mySig.bmp"
         path = "C:\\mySig.bmp";
         img.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
         Response.Write("Image saved successfully to " + path);
         */

        //Send to database

        if (sigBase64.Length > 3000)
        {
            using (SqlConnection con = new SqlConnection(constring))
            {
                con.Open();
                string sql = "insert into SakalPicturecontrol (firstname,lastname,email,base64ImageString) Values (@firstname,@lastname,@email,@base64ImageString)";
                try
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        cmd.Parameters.AddWithValue("@firstname", firstname);
                        cmd.Parameters.AddWithValue("@lastname", lastname);
                        cmd.Parameters.AddWithValue("@email", email);
                        cmd.Parameters.AddWithValue("@base64ImageString", base64ImageString);
                        cmd.ExecuteNonQuery();
                        Response.Write("Information has been saved Correctly..");
                        con.Close();
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("Error: " + ex.ToString());
                }
            }
        }

    }
    else
    {
        //signature has not been returned successfully!
    }
}

我收到此错误,String Reference not set to instance of String。我想念什么吗?我可能无法正确执行某些操作吗?

修改

当我执行Shift + F9组合键时,我会得到这张图片

Error + Exception

修改

HTML部分如下所示:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SignatureCaptureForm.aspx.cs" Inherits="SakalGBSignatureProcess.SignatureCaptureForm" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 20px;
        }
        .auto-style4 {
            left: 0px;
            width: 424px;
            top: 0px;
            height: 204px;
        }
        .auto-style5 {
            width: 344px;
        }
        .auto-style6 {
            width: 509px;
            text-align: right;
        }
        .auto-style7 {
            width: 90%;
        }
        .auto-style8 {
            width: 328px;
        }
        .auto-style10 {
            width: 317px;
        }
    </style>

    <script type="javascript">
        function OnClear()
        {
            document.TopazForm.SigPlus1.ClearTablet(); //Clears the signature, in case of error or mistake
        }

        function onSign()
        {
            document.TopazForm.SigPlus1.TabletState = 1; 
        }

        function onSave()
        {
            if(document.TopazForm.username.value == "")
            {
               alert("Please Username Before Continuing...");
               return false;
            }

            if(document.TopazForm.SigPlus1.NumberOfTabletPoints > 0)
            {
               document.TopazForm.SigPlus1.TabletState = 0; //Turns tablet off
               document.TopazForm.SigPlus1.AutoKeyStart();

               document.TopazForm.SigPlus1.AutoKeyData = document.TopazForm.username.value;
               //pass here the data you want to use to encrypt the signature
               //this demo simply encrypts to the name typed in by the user
               //you'll probably want to make sure your encryption data you use is
               //more useful...that you encrypt the signature to the data important
               //to your app, and what the client has agreed to

               document.TopazForm.SigPlus1.AutoKeyFinish();
               document.TopazForm.SigPlus1.EncryptionMode = 2;
               document.TopazForm.SigPlus1.SigCompressionMode = 1; 

               document.TopazForm.hidden.value = document.TopazForm.SigPlus1.SigString;
               //pass the signature ASCII hex string to the hidden field,
               //so it will be automatically passed when the page is submitted

               document.TopazForm.submit();
            }
            else
            {
               alert("Please Sign Before Continuing...");
               return false;
            }


        }
    </script>
</head>
<body>
    <form id="TopazForm" name="TopazForm" method="post" action="SignatureCaptureForm.aspx" />
        <div>
            <table class="auto-style7">
                <tr>
                    <td class="auto-style6" style="font-family: 'Century Gothic'; font-size: medium; font-weight: normal; font-style: normal; color: #202020">&nbsp;</td>
                    <td class="auto-style1">&nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style6" style="font-family: 'Century Gothic'; font-size: medium; font-weight: normal; font-style: normal; color: #202020">Username: </td>
                    <td class="auto-style1">&nbsp;</td>
                    <td>
                        <INPUT id="username" type="text" name="username" class="auto-style8"></td>
                </tr>
                <tr>
                    <td class="auto-style6" style="font-family: 'Century Gothic'; font-size: medium; font-weight: normal; font-style: normal; color: #202020">First Name : </td>
                    <td class="auto-style1">&nbsp;</td>
                    <td>
                        <input id="firstname" class="auto-style8" type="text" name="firstname" runat="server"/></td>
                </tr>
                <tr>
                    <td class="auto-style6" style="font-family: 'Century Gothic'; font-size: medium; font-weight: normal; font-style: normal; color: #202020">Last Name : </td>
                    <td class="auto-style1">&nbsp;</td>
                    <td>
                        <input id="lastname" class="auto-style8" type="text" name="lastname" runat="server" /></td>
                </tr>
                <tr>
                    <td class="auto-style6" style="font-family: 'Century Gothic'; font-size: medium; font-weight: normal; font-style: normal; color: #202020">Email: </td>
                    <td class="auto-style1">&nbsp;</td>
                    <td>
                        <input id="email" class="auto-style8" type="text" name="email" runat="server"/></td>
                </tr>
                <tr>
                    <td class="auto-style6" style="font-family: 'Century Gothic'; font-size: medium; font-weight: normal; font-style: normal; color: #202020">Passport Photograph:</td>
                    <td class="auto-style1">&nbsp;</td>
                    <td>
                        <asp:Image ID="passport" runat="server" Height="164px" Width="170px" />
                    </td>
                </tr>
                <tr>
                    <td class="auto-style6" style="font-family: 'Century Gothic'; font-size: medium; font-weight: normal; font-style: normal; color: #202020">&nbsp;</td>
                    <td class="auto-style1">&nbsp;</td>
                    <td>
                        <asp:FileUpload ID="FileUpload1" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td class="auto-style6" style="font-family: 'Century Gothic'; font-size: medium; font-weight: normal; font-style: normal; color: #202020">Signature Capture:</td>
                    <td class="auto-style1">&nbsp;</td>
                    <td><OBJECT id="SigPlus1"
                            classid="clsid:69A40DA3-4D42-11D0-86B0-0000C025864A" name="SigPlus1" class="auto-style4">
                            <PARAM NAME="_Version" VALUE="131095">
                            <PARAM NAME="_ExtentX" VALUE="11218">
                            <PARAM NAME="_ExtentY" VALUE="5398">
                            <PARAM NAME="_StockProps" VALUE="9">
                        </OBJECT></td>
                </tr>
                <tr>
                    <td class="auto-style6" style="font-family: 'Century Gothic'; font-size: medium; font-weight: normal; font-style: normal; color: #202020">&nbsp;</td>
                    <td class="auto-style1">&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style6" style="font-family: 'Century Gothic'; font-size: medium; font-weight: normal; font-style: normal; color: #202020">&nbsp;</td>
                    <td class="auto-style1">&nbsp;</td>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <table style="width:100%;">
                            <tr>
                                <td class="auto-style5">
                                    <input id="SignBtn" type="button" value="Sign"  onclick="onSign();"/>&nbsp;
                                    <input id="ClearBtn" type="button" value="Clear" onclick="OnClear();" /></td>
                                <td>
                                    <input id="Button1" type="button" value="Save" onclick="onSave();" />
                                    <INPUT id="hidden" type="hidden" name="hidden"></td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

和后端(后面的代码)看起来像这样

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace SakalGBSignatureProcess
{
    public partial class SignatureCaptureForm : System.Web.UI.Page
    {
        string base64ImageString;
        string constring = WebConfigurationManager.ConnectionStrings["MYCON"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Put user code to initialize the page here
            Topaz.SigPlusNET sigObj = new Topaz.SigPlusNET();
            sigObj.AutoKeyStart();

            //use the same data to decrypt signature
            sigObj.SetAutoKeyData(Request.Form["username"]);

            sigObj.AutoKeyFinish();
            sigObj.SetSigCompressionMode(1);
            sigObj.SetEncryptionMode(2);

            //Now, get sigstring from client
            //Sigstring can be stored in a database if 
            //a biometric signature is desired rather than an image
            sigObj.SetSigString(Request.Form["hidden"]);
            if (sigObj.NumberOfTabletPoints() > 0)
            {
                sigObj.SetImageFileFormat(0);
                sigObj.SetImageXSize(500);
                sigObj.SetImageYSize(150);
                sigObj.SetImagePenWidth(8);
                sigObj.SetJustifyX(5);
                sigObj.SetJustifyY(5);
                sigObj.SetJustifyMode(5);
                System.Drawing.Image img = sigObj.GetSigImage();
                base64ImageString = ImageToBase64(img, System.Drawing.Imaging.ImageFormat.Jpeg);
                /* String path;
                 //path = System.AppDomain.CurrentDomain.BaseDirectory & "mySig.bmp"
                 path = "C:\\mySig.bmp";
                 img.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
                 Response.Write("Image saved successfully to " + path);
                 */

                //Send to database

                if (base64ImageString.Length > 3000)
                {
                    using (SqlConnection con = new SqlConnection(constring))
                    {
                        con.Open();
                        string sql = "insert into SakalPicturecontrol (firstname,lastname,email,base64ImageString) Values (@firstname,@lastname,@email,@base64ImageString)";
                        try
                        {
                            using (SqlCommand cmd = new SqlCommand(sql, con))
                            {
                                cmd.Parameters.AddWithValue("@firstname", firstname);
                                cmd.Parameters.AddWithValue("@lastname", lastname);
                                cmd.Parameters.AddWithValue("@email", email);
                                cmd.Parameters.AddWithValue("@base64ImageString", base64ImageString);
                                cmd.ExecuteNonQuery();
                                Response.Write("Information has been saved Correctly..");
                                con.Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            Response.Write("Error: " + ex.ToString());
                        }
                    }
                }

            }
            else
            {
                //signature has not been returned successfully!
            }
        }

       /* protected void Button2_Click(object sender, EventArgs e)
        {

        }*/

        public string ImageToBase64(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }

      /*  protected void Button3_Click(object sender, EventArgs e)
        {

        }*/
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在html中将文本框的名称设置为用户名,或者需要在此行中使用正确的名称:

sigObj.SetAutoKeyData(Request.Form["username"]);