Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Please help me I just want to save the data from DataGridView to SqlServer on click of the submit button. Please I try all possible thing and even search in google but I am not getting the answer.

I am using C# 2008 window application and SqlServer 2005 Express


[edit]Just a tidy up... - OriginalGriff[/edit]


HI


<br />
I have completed half task the data is saving in sql but only one i want all data should save in sql 



(is there a option to do OR any other way)


Thanks in Advance :rose::rose::rose:
Posted
Updated 17-Nov-10 1:01am
v6
Comments
Toniyo Jackson 16-Nov-10 7:25am    
do you want to save all rows or only single row?
aayu 16-Nov-10 7:40am    
all the rows

Hi,


First you need to create a stored procedure.

Something like this
C#
For I = 0 To DataGridView.RowCount - 1
myCommand.Parameters.Add(@One;, Grid.items(i).value)
myCommand.Parameters.Add(@two;,  Grid.items(i).value) 
mycommand.executenonquery()
Next    



Just try this.

It worked fine in my project.
It works only, at I never set the datagrid datasource value from any other Dataset.



regards,
saran.
 
Share this answer
 
v2
Comments
@nuraGGupta@ 17-Nov-10 6:28am    
This is not a stored procedure. Is this?
Am i wrong?
use sqlbulkcopy for save grid data into database.
Steps :
1. Save Griddata into datatable
2. Use sqlbulkcopy to insert data

C#
public bool InserData(DataTable insertDt)
    {
// insertDt DataTable From UI
        using (SqlConnection objConn = new SqlConnection(objDBTrans.ConnectionString))
        {
            objConn.Open();
            using (SqlBulkCopy objBulkCopy = new SqlBulkCopy(objConn.ConnectionString, SqlBulkCopyOptions.FireTriggers))
            {
                objBulkCopy.DestinationTableName = "real_Propertytype";
               // Arvind W: Column Mapping with DataBase Table
                objBulkCopy.ColumnMappings.Add("Code", "code");

                objBulkCopy.WriteToServer(insertDt);
                objConn.Close();
                return true;
            }
        }
        return false;
    }

I hope this is helpful for u ...:thumbsup:
Arvind Wanjari....
 
Share this answer
 
v2
Comments
aayu 16-Nov-10 7:49am    
well i have call the data from one table and want to save the data to another table but i need to update one row in that table can you tell me any other way where this logic will work
Arvind Wanjari 16-Nov-10 11:01am    
u create another method for that & execute after above method return true.
u can also use tansaction in asp.net
Aarti wrote:

well i have call the data from one table and want to save the data to another table but i need to update one row in that table can you tell me any other way where this logic will work.


My Answer:

When you need to update only one row, then why are you calling all the rows and writing back all the rows again from frontend. (This will only increase the load and time on the machine).


You can do it otherwise by calling only that row from the database, which needs to be updated, and then writing back it to a new table, In the stored procedure you can simply write a command to write all the data from one table to another table leaving one single row, which you have edited and updated from the front end.This will decrease complexity and increase performance also.
 
Share this answer
 
Comments
aayu 17-Nov-10 6:30am    
@Anu : i dont want to save only one row there are more then 10 to 15 row to be stored. And for editing i have kept the button first i need to store all the rows in the table
@nuraGGupta@ 17-Nov-10 6:45am    
I know you want to save more than one rows, but editing needs to be done in only one row at a time.First you need to store all the rows in a table.
You can write a stored procedure or excecute a code even from the front end like this :

INSERT INTO table2 (ID, NAME, DEPT, JOB) //for particular columns
SELECT ID, NAME, DEPT, JOB
FROM table1

OR


INSERT INTO table2 //for all columns
SELECT * FROM table1.

This way all the rows will be inserted into another table. After that, you can update a single row anyhow.

By the way, this time I dont see any roses offered..[:D]
aayu 17-Nov-10 7:00am    
go through this link http://www.codeproject.com/script/Membership/View.aspx?mid=7100862 and see the second message you will come to know what i want...

from that code i can just save one row at a time but i want all the row to be save :(
@nuraGGupta@ 17-Nov-10 7:06am    
So, that means you did it. I can see that this is writing only one row to another table.Where is the code to transfer all the rows from one table to another?
@nuraGGupta@ 17-Nov-10 7:10am    
If you still want to know the method to insert all the rows, just do one thing, before inserting the edited row, run the command I wrote above, fill the datagrid from new table, and then update the database(the new table) using your code. This is the costliest method to do so, if you dont want to use a stored procedure. Using an stored procedure may help you avoid making useless postbacks.
Hi,

You can use SQLDataadapter object for updating data to database.
 
Share this answer
 

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