0

在我的项目中,我正在上传一些文件 SQL Server,当文件上传到表Table1中时,另一个函数将检查表Table1中的文件,并通过从另一个函数获取随机数或字符将随机数或字符插入表Table2它生成并将其作为字符串返回,现在我的问题是文件正确上传并保存在Table1中,但是当Table1插入文件时,另一个插入随机数/字符的函数启动但抛出异常我的代码是,

 public class Upload : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/plain";
    context.Response.Expires = -1;

    try
    {
        HttpPostedFile postedFile = context.Request.Files["Filedata"];

        string savepath = "";
        string tempPath = "";
        tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"];
        savepath = context.Server.MapPath(tempPath);
        string filename = postedFile.FileName;
        string ext = Path.GetExtension(filename);
        string contenttype = String.Empty;
        switch (ext)
        {

            case ".doc":

                contenttype = "application/vnd.ms-word";

                break;
            case ".docx":

                contenttype = "application/vnd.ms-word";

                break;

            case ".pdf":

                contenttype = "application/pdf";

                break;

        }

        context.Response.StatusCode = 200;
        byte[] b = ReadFile(postedFile);
        ExamManagement.SP.QUESTIONPAPER_SP_UPLOAD(filename, contenttype,b).Execute();
        trigger();

    }

    catch (SqlException exp)
    {
        if (exp.Message.Contains("PK_answerkey"))
        {
            context.Response.Write("File Already Uploaded.....");
        }
    }
    catch (Exception ex)
    {
        context.Response.Write("Error: " + ex.Message);
    }


}

public bool IsReusable {
    get {
        return false;
    }
}
private byte[] ReadFile(HttpPostedFile fObj2)
{
    byte[] data = new Byte[fObj2.ContentLength];
    fObj2.InputStream.Read(data, 0, fObj2.ContentLength);
    return data;
}
protected void ClientMessaging(string msg)
{
    String script = String.Format("alert('{0}');", msg);
    Anthem.Manager.IncludePageScripts = true;

}
protected void trigger()
{
    try
    {
        DataSet ds = ExamManagement.SP.Questionpaper_SP_Selectall().GetDataSet();
        if (ds.Tables[0].Rows.Count > 0)
        {
            string a = RandomNumberGenerator(4);
            string b = RandomNumberGenerator(4);
            string c = RandomNumberGenerator(4);
            ExamManagement.SP.Passkey_insert(a, b, c).Execute();
        }
    }
    catch
    {
        throw; **// Exception Was Thrown Here**
    }



}
public static string RandomNumberGenerator(int length)
{
    System.Security.Cryptography.RandomNumberGenerator rng = System.Security.Cryptography.RandomNumberGenerator.Create();

    char[] chars = new char[length];

    //based on your requirment you can take only alphabets or number 
    string validChars = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXzZ";

    for (int i = 0; i < length; i++)
    {
        byte[] bytes = new byte[1];
        rng.GetBytes(bytes);

        Random rnd = new Random(bytes[0]);

        chars[i] = validChars[rnd.Next(0, 61)];
    }

    return (new string(chars));
}

有人可以帮帮我吗...在此先感谢....代码的这一部分出现错误...

     protected void trigger()
{
    try
    {
        DataSet ds = ExamManagement.SP.Questionpaper_SP_Selectall().GetDataSet();
        if (ds.Tables[0].Rows.Count > 0)
        {
            string a = RandomNumberGenerator(4);
            string b = RandomNumberGenerator(4);
            string c = RandomNumberGenerator(4);
            ExamManagement.SP.Passkey_insert(a, b, c).Execute();
        }
    }
    catch
    {
        throw; **// Exception Was Thrown Here**
    }



}
public static string RandomNumberGenerator(int length)
{
    System.Security.Cryptography.RandomNumberGenerator rng = System.Security.Cryptography.RandomNumberGenerator.Create();

    char[] chars = new char[length];

    //based on your requirment you can take only alphabets or number 
    string validChars = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXzZ";

    for (int i = 0; i < length; i++)
    {
        byte[] bytes = new byte[1];
        rng.GetBytes(bytes);

        Random rnd = new Random(bytes[0]);

        chars[i] = validChars[rnd.Next(0, 61)];
    }

    return (new string(chars));
}

Passkey_insert SP 是,

   Create Procedure Passkey_insert
    (
     @red varchar(100),
     @green varchar(100),
     @blue varchar(100)
     )
   as
 BEGIN
 BEGIN TRY
 Insert into Passkeys(Red,Green,Blue) values (@red,@green,@blue)
 END TRY
 BEGIN CATCH                
 IF @@TRANCOUNT > 0                
 ROLLBACK                
 DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int                
 SELECT @ErrMsg = ERROR_MESSAGE(),                
 @ErrSeverity = ERROR_SEVERITY()                
 RAISERROR(@ErrMsg, @ErrSeverity, 1)                
 END CATCH 
 END
4

3 回答 3

2

您的validChars长度是 50,但这里是从 0 到 61 的随机数:

chars[i] = validChars[rnd.Next(0, 61)];

将其更改为:

chars[i] = validChars[rnd.Next(0, 51)];
于 2013-05-24T12:06:54.180 回答
1

您将 validChars 设置为 50 个字符的数组,然后使用 rnd.Next(0, 61) 获取数组的索引。根据定义,它可能超过 validChars 的长度。

换行:

chars[i] = validChars[rnd.Next(0, 61)];

...改为:

chars[i] = validChars[rnd.Next(0, validChars.Length)];

(请注意,Next() 函数的第二个参数是独占的,因此您将使用 0 到 49 的范围来匹配您的数组)

于 2013-05-24T12:06:08.133 回答
0

您的 validChar 数组只有 50 个字符宽,但您正在使用 validChars[rnd.Next(0, 61)] 查找 char 字符

改变

chars[i] = validChars[rnd.Next(0, 61)];

chars[i] = validChars[rnd.Next(0, validChars.Length)];
于 2013-05-24T12:05:40.877 回答