Hi There,
I'm new to C#. However, my task is 2 read a textfile and store the data in database. But, when reading, i only need 2 read the 1st 2 element of each line leaving the header.
Reading the whole line and storing it in database is possible. Bt aftr reading, i dont understand hw 2 omit the rest of the elements of each line except 4 first 2. Elements of each line are seperated by "|". I have commented on the following code showing where the exception comes up and what it is. So can sm1 please, help me to solve this problm?
Thanking you in advance.
string[] lines = new string[2];//array to store values read from text file
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);//Calling the connection string from web.config
conn.Open();
StreamReader reader = new StreamReader(ConfigurationSettings.AppSettings["CustFilePath"].ToString());
string line = string.Empty;
while ((line = reader.ReadLine()) != null)//reading file line by line
{
if (line != "AccountName|Accountcode|ParentAccount|Contact|RelationshipType|Phone|Fax No|AddressName|AddressLine1|AddressLine2|AddressLine3|Suburb|State|Postcode|Country|RepCode|Category|CreditLimit|PaymentTerm|PriceCategory|IndustryCode|AnnualRevenue")
{
lines = line.Split('|');//spliting each line by "|" sign
//saving into database
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO Customer VALUES(@CustCode,@CustName)";
cmd.Connection = conn;
cmd.Parameters.Add("@CustCode", SqlDbType.VarChar, 15).Value = lines[0];
//On the below line is where i get the exception saying, "Index was outside the bounds of the array."
cmd.Parameters.Add("@CustName", SqlDbType.VarChar, 75).Value = lines[1];
cmd.ExecuteNonQuery();
}
}
conn.Close();