指数数组的边界之外

时间:2013-05-24 12:00:46

标签: c# sql-server

在我的项目中我正在上传一些文件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

3 个答案:

答案 0 :(得分:2)

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

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

将其更改为:

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

答案 1 :(得分: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的范围,与您的数组匹配)

答案 2 :(得分:0)

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

更改

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

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