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 errorCould 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. |
Thanks
NelPosted Jul 22, 2014, 9:33 AM
JIgnesh, I think it loads correctly, since when I run the application I see it the table (gridview) filled with data from the csv file
Here is that part of the code
protected void Page_Load(object sender, EventArgs e)
{
int nRows = 0;
conn = new SqlConnection("Data Source=...;Initial Catalog=incidents;Persist Security Info=True;User ID=...;Password=...");
dt.Clear();
if (!Page.IsPostBack)
{
try
{
var line = reader.ReadLine();
var values = line.Split(delimit);
foreach (var dc in values)
{
dt.Columns.Add(dc);
}
reader = new StreamReader(File.OpenRead(@"\\path\Incidents1.csv"));
while (!reader.EndOfStream)
{
int k = 0;
values = reader.ReadLine().Split(',');
row = dt.NewRow();
while (k < (dt.Columns.Count))
{
row[k] = values[k];
k++;
}
if (nRows != 0)
{ dt.Rows.Add(row); }
nRows++;
}
GridView2.DataSource = dt;
GridView2.DataBind();
NelPosted Jul 22, 2014, 9:32 AM
Yes Pradeep,
I have loaded it in Page_Load in
if (!Page.IsPostBack) block inside the Page_load
Jignesh TrivediPosted Jul 22, 2014, 2:35 AM
Hi,
Is your grid view data load correctly...
what is value of clspublic.GetConnectionString() function?
your code is look ok...
first of all comment this code and check whether your grid view load from csv file correctly?
Pradeep ShetPosted Jul 22, 2014, 1:13 AM