Question Convert dataGridView data in to text

andrewmanuja

Well-known member
Joined
May 30, 2019
Messages
75
Programming Experience
Beginner
Hi,

I got an issue with converting dataGridView Data in to text prior to save the data into a SQL DB.

Basically, I want to use the parameters to pass the data into the DB and finding it bit difficult to work around that.
My code is as follows;
 
If you had bound to your DataGridView using a DataSet, then you could use the DataAdapter class to perform the database inserts/updates/deletes for your.

Alternatively, you don't need to extract the text from the DataGridView. You just need to pass the cell values as SqlParameter's to your SqlCommand that you are using to update your database. If you are extracting the text chances are you are building a string on the fly and that means you are setting yourself up for a SQL injection attack. Using SQL parameters will help protect you.
 
As suggested, you should be using a DataTable. Create one with the appropriate schema first, then bind it to the grid, preferably via a BindingSource. You can then configure a data adapter with an appropriate InsertCommand and then a single call to its Update method will save the lot. You can either create the schema manually or you can call FillSchema on the data adapter with an appropriate SelectCommand. Check out my ADO.NET examples here.
 
Hi All,

Thank you very much for your comments.

Was able to rectify the issue using your valuable comments.

Kind regards,

Andrew
 
Back
Top Bottom