Nel

Nel

  • NA
  • 716
  • 1m

Inserting data from GridView into SQL database table

Jul 21 2014 5:02 PM

Hi,

I want to insert the data from a GridView into SQL table. I have this code

foreach (GridViewRow GVRow in GridView2.Rows)

{

string ID = GVRow.Cells[1].Text;

string DateTim = GVRow.Cells[2].Text;

string Location = GVRow.Cells[3].Text;

string IncidentType = GVRow.Cells[4].Text;

string Direction = GVRow.Cells[5].Text;

string LaneBlockage = GVRow.Cells[6].Text;

string CCTV = GVRow.Cells[7].Text;

string SignalTimingChanges = GVRow.Cells[8].Text;

string Benefits = GVRow.Cells[9].Text;

string Latitude = GVRow.Cells[10].Text;

string Longitude = GVRow.Cells[11].Text;

conn = new SqlConnection("Data Source=...;Initial Catalog=incidents;Persist Security Info=True;User ID=...;Password=...");

SqlConnection scn = new SqlConnection(clspublic.GetConnectionString());

using (conn)

{

SqlCommand scm = new SqlCommand();

scm.Connection = conn;

scm.CommandText = @"insert into TrafInc1 (ID, DateTim, Location, IncidentType, Direction, LaneBlockage,CCTV, SignalTimingChanges, Benefits, Latitude, Longitude)

VALUES (@ID,@DateTime,@Location,@IncidentType,@Direction,@LaneBlockage,@CCTV,@SignalTimingChanges,@Benefits,@Latitude,@Longitude)";

scm.Parameters.AddWithValue("@ID", ID.ToString());

scm.Parameters.AddWithValue("@DateTime", DateTim);

scm.Parameters.AddWithValue("@Location", Location);

scm.Parameters.AddWithValue("@IncidentType", IncidentType);

scm.Parameters.AddWithValue("@Direction", Direction);

scm.Parameters.AddWithValue("@LaneBlockage", LaneBlockage);

scm.Parameters.AddWithValue("@CCTV", CCTV);

scm.Parameters.AddWithValue("@SignalTimingChanges", SignalTimingChanges);

scm.Parameters.AddWithValue("@Benefits", Benefits);

scm.Parameters.AddWithValue("@Latitude", Latitude.ToString());

scm.Parameters.AddWithValue("@Longitude", Longitude.ToString());

scm.ExecuteNonQuery();

}

}

 but when I run it I get this error

Could not find file 'C:\Program Files (x86)\IIS Express\Incidents1.csv'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\Program Files (x86)\IIS Express\Incidents1.csv'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
 
Incidents1.csv is the file from where the GirdView2 is populated with data. 
Can anybody help me please how to solve this?
Thanks 

Answers (4)