GridView insert update delete in asp.net - Part 23
Вставка
- Опубліковано 8 лют 2025
- Link for csharp, asp.net, ado.net, dotnet basics and sql server video tutorial playlists
/ kudvenkat
Link for text version of this video
csharp-video-tu...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our UA-cam channel. Hope you can help.
/ @aarvikitchen5572
In this video we will discuss about performing an insert, update and delete on asp.net gridview control using sqldatasource. We will be using tblEmployee table for this demo. SQL script to create and populate this table with sample data is available in Part 13 of asp.net gridview tutorial.
Create an asp.net web application. Drag and drop a gridview and a sqldatasource control on WebForm1.aspx.
Configure "SqlDataSource1" control
1. Right click on "SqlDataSource1" control and select "Show Smart Tag"
2. Now click on "Configure Data Source" link
3. Select connection string, from the dropdownlist on "Choose your data connection" screen. You need to have a connection string specified in web.config file.
4. Click Next
5. On "Configure the Select Statement" screen, select "tblEmployee" table from dropdownlist.
6. Click on "Advanced" button
7. Select "Generate INSERT, UPDATE and DELETE statements" checkbox and click OK
8. Click Next and Finish
Drag and drop gridview control on WebForm1.aspx. Now let us associate "SqlDataSource1" control with "GridView1" control
1. Right click on "GridView1" control and select "Show Smart Tag"
2. Select "SqlDataSource1" from "Choose Data Source" dropdownlist
3. Select "Enable Deleting" and "Enable Editing" checkboxes. At this point "Delete" and "Edit" buttons should appear on the gridview control.
We will be using gridview control's footer for inserting a new record. Set "ShowFooter" property of the GridView1 control to "true". This can be done from the properties window, or in the HTML.
By default GridView control has generated bound fields to display EmployeeId, Name, Gender and City columns.
We need to convert these bound fields into template fields. This can be done very easily using the designer.
1. On the "GridView Tasks" pane click on "Edit Columns" link button.
2. Select "EmployeeId" from "Selected Fields" section and click on "Convert this field into a template field" link
3. Do the same for Name, Gender and City
Now modify the template fields in the HTML. Please note
1. In every TemplateField, along with EditItemTemplate and ItemTemplate, we also need FooterTemplate.
2. A dropdownlist is used in EditItemTemplate and FooterTemplate of "Gender" template field, instead of a textbox control.
3. To validate data during edit and insert operations, notice that, we are using validation controls, in EditItemTemplate and FooterTemplate.
4. A linkbutton is used in the footer template of "EmployeeId" template field, to enable the user to insert a new employee row
5. We have used ValidationGroup="Insert" for all the validation controls in FooterTemplates. LinkButton's ValidationGroup is aslo set to "Insert", so that all the validation controls in in FooterTemplates are fired only on Insert LinkButton click.
6. We have set LinkButton's OnClick="lbInsert_Click".
7. After the closing tag of GridView, notice that we are using 2 validationsummary controls, to display all the validation messages in one place. ValidationSummary1 control's ValidationGroup is set to "Insert". ValidationSummary1 control displays all insert related validation errors, and edit related validation errors are displayed using ValidationSummary2 control.
Finally copy and paste the following event handler method in WebForm1.aspx.cs
protected void lbInsert_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["Name"].DefaultValue =
((TextBox)GridView1.FooterRow.FindControl("txtName")).Text;
SqlDataSource1.InsertParameters["Gender"].DefaultValue =
((DropDownList)GridView1.FooterRow.FindControl("ddlGender")).SelectedValue;
SqlDataSource1.InsertParameters["City"].DefaultValue =
((TextBox)GridView1.FooterRow.FindControl("txtCity")).Text;
SqlDataSource1.Insert();
}
Thank you soooo much for this!
+mcfawknuts Thanks a lot for stopping by to provide your feedback. This means a lot to me. I am really glad you found the video tuorials useful.
All the Pragim Tech video tutorials are organised in to playlists, which help you find the videos you are looking for easily
ua-cam.com/users/kudvenkatplaylists?view=1&sort=dd
You can also order DVDs or download all the videos and slides for offline viewing using the link below
www.pragimtech.com/kudvenkat_dvd.aspx
Image version of the slides and text Version of the videos are on my blog
csharp-video-tutorials.blogspot.com
Tips to effectively use our free channel.
ua-cam.com/video/y780MwhY70s/v-deo.html
To receive email alerts, when new videos are uploaded, please subscribe to my youtube channel.
ua-cam.com/users/kudvenkat
If you like these videos, please click the THUMBS UP button below the video.
We like to see these free video tutorials helping others as well. Please share the link with your friends and family who you think would also benefit from them.
Thanks
Venkat
Worsted
I had some problems with the Insert function not working. I believe that it had to do with not using a "protected" access modifier. it works now. And, thank you, Professor Venkat. You are the best, simply the best. I have watched all your videos: C#, asp.net, ado.net and SQL Server. My career has been enhanced & enabled. I cannot thank you, enough.
This was literally the easiest and the most efficient method anyone could have ever implemented. Thank you for sharing this with us. Helped Alot!
You are a hero. My course project was finished because of your guide.Thank you very much.
Hearty congratulations on successful completion of your course project. Very glad the videos helped. Thank you very much for taking the time to give feedback. All the very best with everything you do.
Your videos are very helpful ! Most i know from ASP.NET i have learned in your videos. Great work ! Thank you.
Thank you very much for taking time to give feedback. For email alerts, when new videos are uploaded, please subscribe to my channel. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video.
this is so useful man! you've just upgraded humanity in a whole new different level.. hope i could see you in person.. (im serious, im your number 1 fan)
Even though this video came out in 2013, it still came in very useful for finishing my assignment. Thank you.
hello venkat, Thank you very much.i understood and i could implement according my requirement .without ignoring it as a silly question, you answered so well, i truly admire your help.Thank you once again
Thank you so much sir.....Best asp.net video I had ever seen...Hats of your effort..thank you so much.
To receive email alerts, when new videos are uploaded, please subscribe to my youtube channel. May I ask you for a favour. I want these tutorials to be helpful for as many people as possible. Please share the link with your friends and family who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video.
Best tutorial on UA-cam Ever Million time Thanks Sir GOD BLESS you.....!
Hi Sujatha, sure, will record and upload a video on this soon. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.
This videos was great, very helpful to my projects. Sharing knowlodge turn the world better. So, I will do the same sharing what I learned with brazilian folks in portuguese. Thanks, and keep sharing.
Keep up the good work kudvenkat, we really appreciate it. Thanks.
amazing video. best asp .net tutorial series on youtube.
"SelectedIndexChanged" is the default event of GridView control. It is very easy to identify default events of controls in asp.net. Drag and drop a control on the webform, double click on the control, and an event handler method will be generated in the code-behind file automatically for the default event. Hope, you will find this useful. If you like these videos, please click on the THUMBS UP button below the video. Good Luck.
Absolutely great stuff! Thank you Kudvenkat for sharing this! :)
Excellent. Thank you very much Kudvenkat.
Can you please help to just submit record into database using ASP.net.
You are really life saver. I am working almost 5 day to do connection string but your video are simply best.
many many thanks
Thank you. Your tutorials are very helpful.
No doubt, You are a Master of Master
Hi Priya, Here the ID column is an identity column. When we insert a row, we don't have to provide a value for this column. It will be auto-incremented and inserted by sql server automatically. In reality, it is unusual to provide a value for ID field. But if your, project requirement is to provide the value for ID, then we can very easily achieve by including a TextBox next to the insert link button in footer template of ID column. Hope this answers your question.
LEGEND FOR DOING THIS
Thank you Man you are simply Stunning.
Thank you for your reply Kudvenkat, I actually just redid it and worked, thanks a lot :)
Very clearly elaborate each steps.
Hi Vivek, are you talking about doing everything in code using RowCommand, without using datasource controls. If that's the case we will be discussing about it very soon. Please stay tuned.
best video for beginners... thank you so much... really useful👍
This is discussed in GridView tutorial. In the description of this video, I have included the link for ASP .NET, C#, and GridView playlists. Hope you will find these videos useful.
hi venkat , i'm very thankful for the support you are providing for your fellow developers, Iam extremely thanful to you , and here is my silly question , after seeing the video, ok , here you are giving a demo on insertion, i see that insert button is sitting in the id field , so do we insert the value for that , and if it is a primary key field , it gives an error, is it'nt ,may be its a very stupid question, i dont know,but please tellme how can i give thevalue for the first field
Thanks a lot for your video,I got some knowledge on Insert,Update and Delete in Gridview.
I saw many video the way you explain is very good I saw many other .net video such as lynda.com pluralsight or other but your video is simply great.I have one request for you plz record video on windows database deployment application.in short how can create software with product key and adding .net framework with that windows application which is easily installed another computer.
its really help me a lots! Much love and thank u!!!!!!!!!!
Thank you very much for this good teaching. I have learned a lot from this video.
Hi, Aniruddha, this is easy to fix. The issue is that, validation controls are performing validations on the client, and are not allowing the page to be posted back to the server, so the code in Image Button click can be executed. To fix this, set CausesValidation property of the Image Button to true. Hope this answers your question. If you like these videos, please click on the THUMBS UP button below the video. Please share these videos with your friends on Google pluas and facebook. Good Luck.
a very big Thank you, you deserve more
Thank you for all your videos !
Congratulations, this is a good tutorial
I Want A Really Fast Video Implementation For CRUD In ASP GridView For Quick Revision And This Helps Me. Very Nice Work. And Also Thank You!
best video....usefull for beginersss
Hi sir, your videos are really helping me a lot for preparing interviews.Thanku so much.and one of the interviewer asked ,what is the default event for a gridview control?can you please share me the anser.
Thank you for this tutorial...really helpful
So helpful video tutorial thank you so much it really helped
You helped me a lot ! awesome video !
Thanks! Glad it's still on line!
Thanks, very good, all work perfect!!!
I love the videos. I am new to all this stuff and it is great to see videos that step you through step by step. How would I show the footer of a gridview when there is no data in the gridview? I have a dropdown list box that will show the records of the selected item but when there are no records associated with the listed item, I cannot insert a record because there is no footer.
thank a lot..very very useful and works fine..briefly describe
Very nice and helpful video.
Thank You so much :)
Thank you, i love your videos
Hi There
Thanks for this wonderful project. Would that be possibility the insert row will appear at the top of the grid instead at the bottom of the grid. Can you please reply it is really urgent.
Thanks
Qazafi
amazing as usual :) Thanks Kudvenkat
I solved it. I changed the line relevant the checkbox in the eventhandler like this: SqlDataSource1.InsertParameters["is_admin"].DefaultValue =
((CheckBox)GridView1.FooterRow.FindControl("chkCredential")).Checked.ToString().ToLower();
Hi Priya, you are very welcome. May I ask you for a favour. I am receiving lot of questions everyday from our youtube users, and finding it extremely difficult to answer all the questions in time. I hope, you can help me answer a few questions for which you know the answer. This will help boost your confidence, and also help build your network on youtube.
Can we use the same with otacle database
Thank you very much bro, you re my best mentor
I was able to put the insert link in the first column (same column as edit/update). I think it looks nicer there. In case anyone was wondering if it could be done.
How did you do that?
Good Tutorial,Thank you so much
Very usefull !! Thanks :)
EXCELLENT video ... thank you so much :D
شكرا لك اخي على هاذا الشرح
Thank you very much. Excellent.
Thank you so much for the helpful video. Just a simple question. I would like to edit a row after a filtered GridView, is that possible?
thanx awsome tutorial
Thanks you so much, I like the way u teach, this video is very helpful for my assignments. Thanks....^_^
Very interesting Thank you for such good work. Actually I have replaced the dropdown with a checkbox and I cannot make it working. I would appreciate some hint about how to change the code in the lblInsert to use a checkbox.
I think this is already covered in gridview tutorial. Please check and let me know if you have found it.
good try for understand to other peoples.
Thanks..nice videos!
you are like God..
hey kud, thank you 4 ur vids, they are very helpful, i gotta question and, i was trying 2 edit the template for my gridveiw, i added a drop down list on 1 of the fields, but the drop down list uses an SQLDataSource cuz i need some values from another table, but instead of editing it deletes the record when i pess update
What should i do if i would have to put on the footer row a calendar ( for example for a datetime from database ); how can i write that on the insert_click event ?
Thanks and thanks for ur lessons , its helpin me a lot :)
thanks very help full
Thank you very much :)
great video
Hi Venkat, Thanks for these amazing videos, which are very helpful for us.
I would like to bind Drop Down List with a DataSource but when I click edit I get error similar to "indurnuguri1" mentioned in his post.
Hi Moath, RequiredFieldValidator should support ControlToVaildate attribute, irrespective of VS2010 or VS2012. I am not sure what's wrong at the moment. Can you please include the complete declaration of RequiredFieldValidator. Since angular brackets, cannot be pasted in the comment, use square brackets [ ] instead of angular brackets.
Thanx bro for
help
Hi Kudvenkat, thanks for the video. I just want to know how to show the gridview footer when the dataSource is empty, I mean, inserting the first row, becuase the gridview i this case doesn't show any row.
thank you sooooo much !!!!!!
thank you very much
If I use the way you have showed then is there a way to show the insert linkbutton in the same column as of the Edit/Delete column?
Hi Venkat,
I want to know how to insert records using first row of the gridview not with using footer.
are you using DataReader or DataAdapter to connect to your DataBase?
Sir could you please tell me how to achieve validation group in mvc as you did in this video. I did not find any appropriate article on web.
I have to say really good and useful videos.
But I would like to ask you about DropDownList used in EditItemTemplate. I'm using VS2012 .Net Framework 4.5 and the SelectedValue is missing... Do you have some video about handling DropDownList with code behind?
Excellent (y)
Hi venkat, Thank for this such wonderful videos. I am having one problem. When i select value from Dropdownlist in Gender column at run time, Its getting Disappear. The value in Table also getting deleted.
I will explain Clearly.for example, I am selecting "male" from gender column at run time. When i clicked update . It will be gone. Y?
Thanks in advance.
wow ty nice vid
what a man move fast, me rahtid!
good tutorial
I always enjoy your videos, Professor, but not for anything could I get this to work. After many internet searches, the following did work; setting the properties AppendDataBoundItems="true" and AutoPostBack="True" in the DDL. thoughts?
I love your teaching, but unfortunately my employee has not given me access to an SQL server to develop. Can you show how to do this using Access database table back end?
Great video! tranks:)
For those who had problems with the SelectedValue.. I fixed it by changing the value on my DB For example in my table "Empleados" the first record name is "Peter" but in the Dropdown list doesnt exist the name "Peter". In the dropdownlist there is "Jonh", "Marck",etc but not "Peter". You can fix it backwards, changing the name on the Dropdownlist not in the DB for matching the values. The important thing is that values must match.
kudvenkat
Is is possible to dinamically disable updating some columns? For example I want to disable to update some columns for specified user. Can I do that ?
Hai Venkat Sir. First i would like to say Thanks for your video Tutorials.
Sir i have a Problem. (DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value)
please help me out..
Sandeep Kumar Neelam hi.
did u get help for ur query? if yes please help me too
Rizwana Abbasi No Buddy
Sandeep Kumar Neelam refer here stackoverflow.com/questions/31919133/dropdownlist1-has-a-selectedvalue-which-is-invalid-because-it-does-not-exist-i
good one thanks
I would like to use a second sqlconnection with datavalues to be used in the drop downlist
How can i do that?
btw. thanks for great videos!
Thank you!!!