错误:索引超出了数组的范围 [英] error:index was outside the bounds of the array

查看:85
本文介绍了错误:索引超出了数组的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我为什么会收到此错误?

  protected   void  btnSubscribe_Click( object  sender,EventArgs e)
{
string arrvalues = null ;
for int i = 0 ; i < CBL1.Items.Count; i ++)
{
if (CBL1.Items [i] .Selected)
{
arrvalues = arrvalues + CBL1.Items [i] .Value + < span class =code-string>,;
}

}

if (arrvalues == null
{

}
else
{

if (arrvalues.EndsWith( ))
{
arrvalues = arrvalues.Remove(arrvalues.Length - 1 1 );
}
My_Util Util = new My_Util();
DataSet ds;
ds = Util.CheckusernameMail(txtEmail.Text.Trim());
if (ds.Tables [ 0 ]。Rows.Count > 0
{

Util.SetConnection();
Util.sql_con.Open();
SqlCommand cmd;



cmd = new SqlCommand( update offsale.OS_Mailinglist1
set Area_Of_Interest ='
+ arrvalues + '其中UserName =' +
txtEmail.Text.ToString()+ ',Util.sql_con);
cmd.ExecuteNonQuery();
ClientScript.RegisterStartupScript( this .GetType(), alert
alert('感兴趣的区域已成功更新'); true );

Util.sql_con.Close();

foreach (ListItem item in CBL1.Items)
{
if (item.Selected == true
{
item.Selected = false ;
}
}

txtEmail.Text = ;

btnSubscribe.Enabled = false ;

}
else
{
Util.SetConnection();
Util.sql_con.Open();

cmd = new SqlCommand( 插入

offsale.OS_Mailinglist1(UserName,Area_Of_Interest)值('
+
txtEmail.Text.ToString()+ ',' + arrvalues + '),Util.sql_con);


cmd.ExecuteNonQuery();
ClientScript.RegisterStartupScript( this .GetType(), alert
alert('成功注册您的电子邮件ID和区域兴趣。');
true );

//
ClientScript.RegisterStartupScript( .GetType(), alert alert('注册
你的MailingList密码')
);

Util.sql_con.Close();

订阅 ;

foreach(CBL1中的ListItem项目。项目)
{
if(item.Selected == true)
{
item.Selected = false;
}
}

txtEmail.Text =
;
//Label1.Visible = false;
//asswrd.Visible = false;
//btnSend.Enabled = true;
Label2.Visible = true;
Label2.Text =
选择您感兴趣的领域&

btnSubscribe.Enabled = false ;

}

MailMessage msg = new MailMessage();

StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

htw.RenderBeginTag( HTML);
htw.RenderBeginTag( HEAD);
htw.RenderBeginTag( TITLE);
htw.RenderEndTag();
htw.RenderEndTag();
htw.RenderBeginTag( BODY);
htw.WriteLine( 亲爱的先生/女士,< br>);
htw.WriteLine( 我们非常感谢您输入'MySale -
加入我们的邮件列表< BR>中);


尝试
{
string myString = txtEmail.Text.ToString();

string result1 = myString.Split(' @')[ 1 ];


msg.From = new MailAddress( mysale@usam.in);


msg.To.Add(txtEmail.Text.ToString());

msg.Subject = Mysale-Joining我们的邮件列表 - 致谢;

msg.Body = sw.ToString();
msg.IsBodyHtml = true ;

System.Net.Mail.SmtpClient client = new
System.Net.Mail.SmtpClient( usam.in);

System.Net.NetworkCredential mailauthentication = new
System.Net.NetworkCredential( usam1@usamindia.com usam12usam);


client.UseDefaultCredentials = false ;
client.Credentials = mailauthentication;

client.Send(msg);

ClientScript.RegisterStartupScript( this .GetType(), alert
alert('我们已成功注册您加入我们的邮件
列表的电子邮件ID,您订阅了以下项目');
true );
return ;
}

catch (例外情况)
{
Response.Write(ex.Message);
}

}

} < / br > < / br >

解决方案





你需要在用@符号拆分之前添加条件。

如果返回计数为1则只进一步,否则电子邮件地址不正确。



所以你的代码是,

  var  result = mystring.Split('  @'); 
如果(result.count > = 1
字符串 result1 =结果[ 1 ];



祝你好运


 尝试 
{
string myString = txtEmail.Text.ToString();

string result1 = myString.Split(' @')[ 1 ]; ---->我在这里得到错误因为索引超出了数组的范围



如果你在那里得到一个错误,那么这意味着在那里没有''@''字符文本框 - 为什么我们不能说,但它可能与

 txtEmail.Text =相关; 

在它上面的行。






Split方法返回一个字符串数组。如果指定的分隔符不在提供的字符串中,那么它将只返回一个带有单个元素(第0个索引)的字符串数组。因此,在这样的情况下,无法检索索引等于1的元素。如果您可以保证指定的分隔符在提供的字符串中,则这是正确的。以下其中一项可用于确保



 如果 (mystring.Contains(  @))
{
string result1 = mystring.Split(' @') [ 1 ];
}





  var  result = mystring.Split('  @'); 
if (result.Length > = 1
{
string result1 = mystring.Split('' @')[ 1 ];
}





此外,你不必使用

字符串myString = txtEmail.Text .ToString(); 

as

 txtEmail.Text 

是字符串本身。



问候


please tell me why i am getting this error?

protected void btnSubscribe_Click(object sender, EventArgs e)
{
string arrvalues = null;
for (int i = 0; i < CBL1.Items.Count; i++)
{
if (CBL1.Items[i].Selected)
{
arrvalues = arrvalues + CBL1.Items[i].Value + ",";
}

}

if (arrvalues == null)
{

}
else
{

if (arrvalues.EndsWith(","))
{
arrvalues = arrvalues.Remove(arrvalues.Length - 1, 1);
}
My_Util Util = new My_Util();
DataSet ds;
ds = Util.CheckusernameMail(txtEmail.Text.Trim());
if (ds.Tables[0].Rows.Count > 0)
{

Util.SetConnection();
Util.sql_con.Open();
SqlCommand cmd;



cmd = new SqlCommand("update offsale.OS_Mailinglist1
set Area_Of_Interest='" + arrvalues + "' where UserName='" +
txtEmail.Text.ToString() + "'", Util.sql_con);
cmd.ExecuteNonQuery();
ClientScript.RegisterStartupScript(this.GetType(), "alert",
"alert('Area of Interest Updated Successfully');", true);

Util.sql_con.Close();

foreach (ListItem item in CBL1.Items)
{
if (item.Selected == true)
{
item.Selected = false;
}
}

txtEmail.Text = "";

btnSubscribe.Enabled = false;

}
else
{
Util.SetConnection();
Util.sql_con.Open();

cmd = new SqlCommand("insert into

offsale.OS_Mailinglist1(UserName,Area_Of_Interest)values('" +
txtEmail.Text.ToString() + "','" + arrvalues + "')", Util.sql_con);


cmd.ExecuteNonQuery();
ClientScript.RegisterStartupScript(this.GetType(), "alert",
"alert('Successsfully Registered your Email ID and Area of interest.');",
true);

//
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Register
your Password to MailingList')", true);

Util.sql_con.Close();

Subscribe";

foreach (ListItem item in CBL1.Items)
{
if (item.Selected == true)
{
item.Selected = false;
}
}

txtEmail.Text = "";
//Label1.Visible = false;
//asswrd.Visible = false;
//btnSend.Enabled = true;
Label2.Visible = true;
Label2.Text = "Please select your Area of Interest &

btnSubscribe.Enabled = false;

}

MailMessage msg = new MailMessage();

StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

htw.RenderBeginTag("HTML");
htw.RenderBeginTag("HEAD");
htw.RenderBeginTag("TITLE");
htw.RenderEndTag();
htw.RenderEndTag();
htw.RenderBeginTag("BODY");
htw.WriteLine("Dear Sir/Madam,<br>");
htw.WriteLine("We thankfully acknowledge your entry for ‘MySale –
Joining Our Mailing List’.<br>");


try
{
string myString = txtEmail.Text.ToString();

string result1 = myString.Split('@')[1];


msg.From = new MailAddress("mysale@usam.in");


msg.To.Add(txtEmail.Text.ToString());

msg.Subject = "Mysale-Joining Our Mailing List-– Acknowledgement";

msg.Body = sw.ToString();
msg.IsBodyHtml = true;

System.Net.Mail.SmtpClient client = new
System.Net.Mail.SmtpClient("usam.in");

System.Net.NetworkCredential mailauthentication = new
System.Net.NetworkCredential("usam1@usamindia.com", "usam12usam");


client.UseDefaultCredentials = false;
client.Credentials = mailauthentication;

client.Send(msg);

ClientScript.RegisterStartupScript(this.GetType(), "alert",
"alert('We have successfully registered your Email ID for Joining Our Mailing
List ,you are subscribed with the following item');", true);
return;
}

catch (Exception ex)
{
Response.Write(ex.Message);
}

}

}</br></br>

解决方案

Hi,

You need to add condition before after split with the @ sign.
if the return count is 1 then only proceed further otherwise the email address is incorrect.

so your code would be,

var result = mystring.Split('@');
if(result.count >=1)
string result1 = result[1];


Best luck


try
{
string myString = txtEmail.Text.ToString();

string result1 = myString.Split('@')[1];---->i m getting the error here as index was outside the bounds of array


If you are getting an error there, then it means that there is no ''@'' character in the textbox - why we can''t say, but it may be related to the

txtEmail.Text = ""; 

line above it.


Hi,

Split method returns a string array. If the specified separator character is not in the string provided, then it will just return a string array with a single element (0th index). So in a case like that it is not possible to retrieve an element with index equals 1. This is correct if you can guarantee the specified separator character is in the string provided. One of the following can be used to ensure that,

if (mystring.Contains("@"))
{
    string result1 = mystring.Split('@')[1];
}


or

var result = mystring.Split('@');
if (result.Length >= 1)
{
    string result1 = mystring.Split('@')[1];
}



Also, you don''t have to use

string myString = txtEmail.Text.ToString();

as

txtEmail.Text

is string itself.

Regards.


这篇关于错误:索引超出了数组的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆