473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Saving multiple rows of gridview into database

Hi there, i have a gridview which populated via selected item on my drop down list which I don't have any problem with that. My problem is I am trying to save multiple rows on my gridview into my database, for example I have added 3 items on my gridview which looks like this:

Id | Name
111 | Jack
222 | Nicole
333 | John

Now, I want all that under column Id which is 111, 222 and 333 will be save on my database once I click Save button I am trying the code below but it gives me an error saying "system.data.sqlclient.sqlparameter does not contain a definition for 'Values' and no extension method 'Values' accepting a first argument of type..." :

Expand|Select|Wrap|Line Numbers
  1. SqlConnection con = new SqlConnection("Data Source=GATE-PC\\SQLEXPRESS;Initial Catalog=dbProfile;Integrated Security=True");
  2.         SqlCommand cmdd = new SqlCommand("Insert into profile (Id, profile_id)VALUES(@id, @profile_id", con);
  3.  
  4.         foreach (GridViewRow row in GridView1.Rows)
  5.         {
  6.  
  7.             cmdd.Parameters.Add("@id", SqlDbType.Char, 20, "Id").Values = row.Cells["Id"].Value;
  8.         }
  9.  
  10.         cmdd.CommandType = System.Data.CommandType.Text;
  11.  
  12.         cmdd.Parameters.AddWithValue("@profile_id", txtID.Text);
  13.         con.Open();
  14.         cmdd.ExecuteNonQuery();
My table should be look like this once I am able to save multiple rows from my gridview into my database:

auto_id | Id | profile_id
1 |111 | 101
2 |222 | 101
3 |333 | 101

what am I missing? please help. Im using asp.net with c#. Thanks and God bless
Jun 1 '13 #1
1 15580
Oralloy
988 Expert 512MB
Hello Petro!

Kudos on using parameters to insert values into your SQL!
  1. They prevents SQL injection attacks.
  2. They eliminate the value encoding problems that dynamic SQL has.

First: I see is that your SQL in line 2 is malformed, it should probably be something like this:
Expand|Select|Wrap|Line Numbers
  1.   SqlCommand cmdd = new SqlCommand("INSERT INTO profile(Id, profile_id) VALUES(@id, @profile_id)", con);  
(Added spacing)
(Added a closing parenthesis to the VALUES clause.)
(Did not add a semicolon (";"), this should not affect your work, either way.)

Since it's pretty obvious that you're new to this, I'm going to approach helping you from a teaching perspective, not just writing the code and leaving you ignorant.

Second: It looks like you are a little confused in your logic - it looks like you loop around each of your gridview's rows and try to add a query parameter for the ID, then you try to add the profile-id once at the end.

Rather, you might want to do a complete SQL INSERT command for each row in your gridview. NOTE: some databases allow INSERT command bundling, but that is overkill for what we are doing.

This SqlCommand class, Parameters property manual page has good examples of parameter use. The example uses UPDATE, rather than INSERT, which is immaterial to our needs.

Reading the manual page, you will see that it gives two distinct ways of adding a parameter value to the collection - the first adds parameter @ID, and uses code to provide explicit typing information - the second adds the parameter @demographics, using implicit typing based on the supplied variable.

I suggest that you use the second method of adding parameters as the model for your code.

Third: now that you have a simple example, and a method of manipulating records, write the code to INSERT just the first row from your gridview.

Then, test your code and see how the pieces work together, and what the results are. This will help you become familiar with the class library and its use. Don't forget to include your error detectinon and reporting. People hate silent failures, and you will hate how they report problems - almost all information from system error messages is either ignored, lost, or simply misstated. But, I digress.

Fourth: once you have a single insert working, I think that wrapping a loop around the code will be straightforward.

Make sure to test your code and review the results. Did you test at the boundary conditions (e.g. blank ID values, and repeated ID values)?

Fifth: do you know what transactioning is? If so, you might want to consider whether it is valuable in this circumstance or not.

Ok, before we make a life's project out of this, I'll let you take over.

Good luck!

If you have any questions, or run into any more problems, let me know.

Kind Regards,
Oralloy
Jun 2 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: RotterdamStudents | last post by:
Hello there, i have a strange problem. I can't get php to insert multiple rows at once in a MySQL database. I use the $sql = "INSERT INTO database (a,b,c,d,e) VALUES ('$a', '$b' ,'$c', '$d',...
11
by: UDBDBA | last post by:
Hi: This is a merge questions which has been posted and answered... in my case need more clairification when target table (tableB) matched multiple rows to be updated based on the ON condition...
1
by: Magnus | last post by:
I'm testing walkthrough saving data to a Database (Multiple Tables). http://msdn2.microsoft.com/en-us/library/4esb49b4(VS.80).aspx In the famous Customer/Order example, I'm getting referential...
1
by: srinivasdevakumar | last post by:
MY doubt is how to retrive multiple rows from data base on one condition by using JSP. please tell me
3
by: khairulfnd | last post by:
i want to select multiple rows using a checkbox in a gridview in which it will be posted in another gridview
0
by: takallen | last post by:
Hello, I am designing a web page that is to be used as a data entry form. Each row contains 50 columns, and the web page will display about 25 rows. To save some time from creating so many ...
2
kaleeswaran
by: kaleeswaran | last post by:
hi! i want to know ..how to getting multiple rows from the database using ajax.and i pass these value to the javascript.give me solution... ...
20
idsanjeev
by: idsanjeev | last post by:
hello i want to modify multiple rows in database with select option i am using R.update but it can update only one record in database but i wants to more then one record is modify after submit so...
6
by: =?Utf-8?B?TU1TSkVE?= | last post by:
How to let user delete multi rows from the BindingSource while the SelectionMode Property set to RowHeaderSelect I have in my program datagridview bound it to sql table Throw Bindingsource To...
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.