It shows this error Index was outside the bounds of the array.?

Posted by Sudheep.grandhe under C# on 11/5/2011 | Points: 10 | Views : 2929 | Status : [Member] | Replies : 1
Hi,
Hi,

After debugging the code it shows the error in this particular line.I am underlying the error.please find it.please do the needful.

Error:-
private void btn_add_Click(object sender, EventArgs e)
{
string strondate, strondate1, ondate;
string strinvoicedate, strinvoicedate1, invoicedate2;
//SqlConnection con = new SqlConnection("data source=mars-104;intial catalog=marsweb.imageimpacts;integrated security=true");
DBConnection db = new DBConnection();

SqlConnection con = db.opencon();
SqlCommand cmd = new SqlCommand("insert into tbl_temp (invoiceno,particulars,quantiy,perprice,amount,orderno,compname,invoicedate,ondate,invoiceid)values(@invoiceno,@particulars,@quantity,@perprice,@amount,@orderno,@compname,@invoicedate,@ondate,@invoiceid)", con);
cmd.Parameters.AddWithValue("@invoiceno", txt_invoiceno.Text);
cmd.Parameters.AddWithValue("@particulars", cmb_particulars.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@quantity", txt_quantity.Text);
cmd.Parameters.AddWithValue("@perprice", txt_perprice.Text);
cmd.Parameters.AddWithValue("@amount", txt_amount.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.SelectedItem.ToString());

strinvoicedate = txt_invoicedate.Text;
if (strinvoicedate != "null")
{
char[] separator = new char[] { ' ' };
string[] s1 = strinvoicedate.Split(separator);
string date2 = s1[0].ToString();
if (date2 != "null")
{
char[] separator1 = new char[] { '-' };
string[] s2 = date2.Split(separator1);
string day = s2[0].ToString();
string month = s2[1].ToString();
string year = s2[2].ToString();
strinvoicedate1 = (year + "-" + month + "-" + day);
invoicedate2 = strinvoicedate1;
cmd.Parameters.AddWithValue("@invoicedate", invoicedate2);
}
}
strondate = txt_ondate.Text;
if (strondate !="null")
{
char [] separator=new char [] { ' ' };
string [] s1=strondate.Split (separator );
string date1=s1[0].ToString ();
if (date1 !="null")
{
char [] separator1=new char [] { '-' };
string [] s2= date1 .Split (separator1 );
string day=s2[0].ToString ();
string month=s2[1].ToString ();
string year=s2[2].ToString ();
strondate1 =(year + "-" + month+ "-" +day );
ondate =strondate1 ;
cmd.Parameters .AddWithValue ("@ondate",ondate );
}
}
cmd.Parameters .AddWithValue ("@invoiceid",txt_invoiceno .Text );
SqlDataAdapter da=new SqlDataAdapter (cmd );
con.Open ();
cmd.ExecuteNonQuery ();
con.Close ();
txt_quantity .Text ="";
txt_perprice .Text ="";
txt_amount .Text ="";
netamount ();
loadparticularsdata ();
vatamount ();
}
string month = s2[1].ToString();

Best,
Sudheep.



Responses

Posted by: Blessyjees on: 11/6/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Check the values of array s2. If an array has one value.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response