GridView insert update delete in asp.net - Part 23

preview_player
Показать описание

Link for text version of this video

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 YouTube channel. Hope you can help.

Configure "SqlDataSource1" control
1. Right click on "SqlDataSource1" control and select "Show Smart Tag"
2. Now click on "Configure Data Source" link
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

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.

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();
}
Рекомендации по теме
Комментарии
Автор

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.

stephanieezat-panah
Автор

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.

Csharp-video-tutorialsBlogspot
Автор

This was literally the easiest and the most efficient method anyone could have ever implemented. Thank you for sharing this with us. Helped Alot!

nikhillala
Автор

Your videos are very helpful ! Most i know from ASP.NET i have learned in your videos. Great work ! Thank you.

WolfgangLettko
Автор

You are a hero. My course project was finished because of your guide.Thank you very much.

kalin
Автор

Even though this video came out in 2013, it still came in very useful for finishing my assignment. Thank you.

Tlgte
Автор

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)

mryoso
Автор

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.

Csharp-video-tutorialsBlogspot
Автор

Thank you so much asp.net video I had ever seen...Hats of your effort..thank you so much.

arunaravind
Автор

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.

charlesrabelloable
Автор

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

lopintipriya
Автор

Best tutorial on YouTube Ever Million time Thanks Sir GOD BLESS

johnsongeorge
Автор

amazing video. best asp .net tutorial series on youtube. 

JoeG
Автор

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.

Csharp-video-tutorialsBlogspot
Автор

Keep up the good work kudvenkat, we really appreciate it. Thanks.

TheSplifydadz
Автор

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.

Csharp-video-tutorialsBlogspot
Автор

Absolutely great stuff! Thank you Kudvenkat for sharing this! :)

pqmg
Автор

Thanks a lot for your video, I got some knowledge on Insert, Update and Delete in Gridview.

saialuri
Автор

best video for beginners... thank you so much... really useful👍

sharvaryrane
Автор

Thank you very much for this good teaching. I have learned a lot from this video.

黄桂兰-ok
welcome to shbcf.ru