?
Solved

SQL Error - Saving gridview data to SQL database table

Posted on 2011-05-03
6
Medium Priority
?
342 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:NerishaB
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:kaminda
ID: 35512276
You have to use a proper connection string in this line

Conn.ConnectionString = "ConnectionString";

0
 
LVL 9

Expert Comment

by:kaminda
ID: 35512290
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.
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 35512315
refer : connectionstring.com
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 

Author Comment

by:NerishaB
ID: 35512317
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.
0
 
LVL 9

Accepted Solution

by:
kaminda earned 2000 total points
ID: 35512433
It looks like there are quit a few reasons for this error.

It also can be random network issue. Can you please try and do a reboot and see whether it helps.

Also check this thread http://www.bigresource.com/Tracker/Track-ms_sql-zF9EpGS8/ Which says some issue with .Net connection pooling
0
 

Author Closing Comment

by:NerishaB
ID: 35512561
Think you were right, I restarted and it works fine now.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question