索引超出了数组的范围 - 浏览器上的错误 [英] Index was outside the bounds of the array --error on browser
本文介绍了索引超出了数组的范围 - 浏览器上的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class NewRegistration : System.Web.UI.Page
{
public string GetConnectionString()
{
//sets the connection string from your web config file "ConnString" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void ExecuteInsert(string name, string Lastname, string username, string password, string Repassword, string gender, string address, string Email)
{
string sql = "INSERT INTO dance.Registration (Name, Lastname, Username, Password, Repassword, Gender,Address,Email) VALUES " + " (@Name,@Lastname,@Username,@Password,@Repassword,@Gender,@Address,@Email)";
try
{
SqlConnection conn = new SqlConnection(GetConnectionString());
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[7];
//param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
param[0] = new SqlParameter("@Name", SqlDbType.NVarChar, 50);
param[1] = new SqlParameter("@Lastname", SqlDbType.NVarChar, 50);
param[2] = new SqlParameter("@Username", SqlDbType.NVarChar, 50);
param[3] = new SqlParameter("@Password", SqlDbType.NVarChar, 50);
param[4] = new SqlParameter("@Repassword", SqlDbType.NVarChar, 50);
param[5] = new SqlParameter("@Gender", SqlDbType.NChar, 10);
param[6] = new SqlParameter("@Address", SqlDbType.NVarChar, 100);
param[7] = new SqlParameter("@Email", SqlDbType.NVarChar, 50);
param[0].Value = name;
param[1].Value = Lastname;
param[2].Value = username;
param[3].Value = password;
param[4].Value = Repassword;
param[5].Value = gender;
param[6].Value = address;
param[7].Value = Email;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
//finally
//{
// Conn.close();
//}
}
public static void ClearControls(Control Parent)
{
if (Parent is TextBox)
{ (Parent as TextBox).Text = string.Empty; }
else
{
foreach (Control c in Parent.Controls)
ClearControls(c);
}
}
protected void Createusrbtn_Click(object sender, ImageClickEventArgs e)
{
if (TxtPassword.Text == TxtRePassword.Text)
{
//call the method to execute insert to the database
ExecuteInsert(TxtName.Text,
txtLastName.Text,
TxtUserName.Text,
TxtPassword.Text,
TxtRePassword.Text,
DropDownList1.SelectedItem.Value,
TxtAddress.Text,
txtEmailID.Text);
Response.Write("Record was successfully added!");
ClearControls(Page);
}
else
{
Response.Write("Password did not match");
TxtPassword.Focus();
}
}
}
推荐答案
SqlParameter[] param = new SqlParameter[7];
您指定长度为7的 param
变量,但在尝试将8个值放入数组后。
你应该写
You assign your param
variable with a length of 7, but after you try to put 8 values in the array.
You should write
SqlParameter[] param = new SqlParameter[8];
PS:下次尝试至少:
- 格式化你的代码
- 告诉您哪一行收到错误
- 提出问题
PS: next time, try at least :
- to format your code
- to tell on which row you get the error
- to ask a question
这篇关于索引超出了数组的范围 - 浏览器上的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文