Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Actually i have one Gridview and i have certain values in the grid. My grid view is having more than 30 columns(rows also) that is it contains dates of particular month.

So i have created one table in my SQL with the same column name as i had in my Grid view.

Now i want to copy all my records into the data table(SQL) from grid view.


Gridview1------>tbl.[date_register].

What I have tried:

I have tried it using the for loops and insert query. But still i have to enter all my 31 columns in the query. SO is there any way to mass upload or bulk upload the values from grid view to SQL table?

I meant any way without using for loops and insert query?
pls help me...
Posted
Updated 25-Jul-18 13:30pm

1 solution

It really depends on what data you are using as a data source for your grid view.

SqlBulkCopy Methods (System.Data.SqlClient)[^]

If its one of the data types accepted by the WriteToServer methods you are all set. Otherwise, if its some collection, you can probably use a LINQ Select to convert to one of those types.

Without more information about your data source, I cannot be more specific.
 
Share this answer
 
Comments
Member 13858616 25-Jul-18 21:13pm    
Thanks Eric for your helping mentality.. My data would be like.

Shift Monday(1/2/2018) Tuesday(2/2/2018) Wednesday(3/2/2018) Thursday(4/2/2018)...etc
-------------------------------------------------------------------------------------
1 A B C D
2 E F G H
3 I G K L

My grid data would be like this.. So there is a table with all these fields, So can i just bulk copy it to table?
Eric Lynch 25-Jul-18 22:56pm    
Its more important that I understand the data type used to populate your grid view than it is to know the actual data. Generally, it is uncommon for a grid view to be populated directly, instead it is normally bound to a data source (DataTable, List<t>, etc.). For example, if its bound to a DataTable, this data type can be passed directly (as a parameter) to the WriteToServer method. If it's bound to a List<t>, its comparatively easy to use a LINQ Select/ToArray to create an array of DataRow, which can also be passed directly (as a parameter) to the WriteServer method. If it is neither of these cases, then I would need to understand EXACTLY how you populate the grid view to suggest a best course of action. I would also need the fully qualified name of the data type for the grid view (e.g. System.Windows.Forms.DataGridView) so that we don't waste time. Within the .NET framework, there are a few different ways to represent a grid.
Member 13858616 26-Jul-18 13:18pm    
Actually its is not a windows form application, its a web application. And my grid is not directly populating from a any data sources. Some data are taking from one of my end table and it does some manipulations on it and then some data is generating in the Grid. So data type is like we can consider it as a String for all fields.
Eric Lynch 26-Jul-18 16:10pm    
In that case, I'd populate a System.Data.DataTable and call the System.Data.SqlClient.SqlBulkCopy.WriteToServer passing that as a parameter. I'd offer more help, but you still haven't shared data type for the grid. Telling me its a web app narrows it (somewhat), but don't have any more time to guess.

Here's an example of how to create and use a DataTable…

https://msdn.microsoft.com/en-us/library/ex21zs8x(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900