Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please tell me why i am getting this error?
C#
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>
Posted
Updated 13-Dec-12 6:05am
v2
Comments
[no name] 13-Dec-12 11:54am    
Can you please optimize your code and tell us at which point you are getting the error?
nityasri 13-Dec-12 11:57am    
s sure..
nityasri 13-Dec-12 12:00pm    
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

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,
C#
var result = mystring.Split('@');
if(result.count >=1)
string result1 = result[1];

Best luck
 
Share this answer
 
Comments
nityasri 13-Dec-12 12:36pm    
@amit thank u
C#
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.
 
Share this answer
 
Comments
nityasri 13-Dec-12 12:36pm    
@griff thank u
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,

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

or
C#
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.
 
Share this answer
 
Comments
nityasri 13-Dec-12 12:43pm    
thanks yaar

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900