Link to home
Start Free TrialLog in
Avatar of NerishaB
NerishaBFlag for South Africa

asked on

SQL Error - Saving gridview data to SQL database table

Hi,

I am trying to save gridview data to a database table by means of the following code attached.  I tested the query, and it works fine.  I am getting an error at line:

"cmd.ExecuteNonQuery();"

Error:  A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The handle is invalid.)

protected void Save_Click(object sender, ImageClickEventArgs e)
        {
            foreach (GridViewRow gvr in GridView2.Rows)  
            {
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = "ConnectionString";

                string SqlString = "insert into [Applied_DailyHrs](Name,Employee_ID, DailyHrsDate, Rules SRay_ID, ProductLine, Calc_first_clock, Calc_last_clock, Calc_work_Start, Calc_work_end, Worked_Hrs, Extra_Before, Extra_during, Extra_after, NormalDay) values ("+ "'test2'" + "," + gvr.Cells[0].Text + "," + "'" + gvr.Cells[5].Text  + "' ," + gvr.Cells[1].Text + "," + gvr.Cells[2].Text + "," + "2" + "," + "'" + gvr.Cells[6].Text + "' ," + "'" + gvr.Cells[7].Text + "' ," + "'" + gvr.Cells[8].Text + "' ," + "'" + gvr.Cells[9].Text + "' ," + "'" + gvr.Cells[10].Text + "' ," + "'" + gvr.Cells[11].Text + "' ," +
"'" + gvr.Cells[12].Text + "' ," + "'" + gvr.Cells[13].Text + "' ," + "'" + gvr.Cells[3].Text + "'" + ")"; 

SqlCommand cmd = new SqlCommand(SqlString, Conn);
Conn.Open();
cmd.ExecuteNonQuery();
Conn.Close();
    }

}

Open in new window

Avatar of kaminda
kaminda
Flag of Sri Lanka image

You have to use a proper connection string in this line

Conn.ConnectionString = "ConnectionString";

If it hidden only in this sample, please verify your connection string is correct. This is a connectivity issue you are having with SQL Server.
Avatar of Alpesh Patel
refer : connectionstring.com
Avatar of NerishaB

ASKER

My connectionstring is hidden for security purposes.  I dont know if it is a connectivity issue, as it retrieves the correct data, It just does not save the data to the DB.
ASKER CERTIFIED SOLUTION
Avatar of kaminda
kaminda
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Think you were right, I restarted and it works fine now.