Simple Insert Select Update and Delete in ASP. NET Using MYSQL Database. Introduction. This article explains how to insert Select, Update and Delete data into a My. SQL database from an ASP. NET web application. So, let's proceed with the following procedure: ASP. NET web page. Grid View Data Control and My. The Update method of the DataAdapter is called to resolve changes from a DataSet back to the data source. The Update method, like the Fill method, takes as arguments. Here Mudassar Ahmed Khan has explained with simple examples, how to write Insert, Update and Delete Triggers in SQL Server. This tutorial is applicable for all. Use Microsoft's SQL data platform to create data-centric solutions across mobile devices and desktops for web servers, enterprise servers, and the cloud. SQL Database. Now, open a My. SQLAdmin Page then select "Create A New Table” - > "View" - > ”Table Structure for Table `student`”. CREATE TABLE IF NOT EXISTS `student` ( `SID` int(1. NOT NULL AUTO_INCREMENT, `Name` varchar(1. NOT NULL, `Address` varchar(5. NOT NULL, `Email` varchar(1. NOT NULL, `Mobile` varchar(2. NOT NULL, PRIMARY KEY (`SID`) ) ENGINE=Inno. DB DEFAULT CHARSET=latin. ![]() AUTO_INCREMENT=3. Open your instance of Visual Studio 2. ASP. NET Web application. Name the project “MYSQLCRUDApplication ", as shown in the following figure: In the code behind file (Student. Student. aspx < %@ Page Title="" Language="C#" Master. Page. File="~/Site. Master" Auto. Event. Wireup="true" Code. Behind="Student. aspx. Inherits="MYSQLCRUDApplication. Student" %> < asp: Content ID="Content. ![]() Content. Place. Holder. ID="title. Content" runat="server"> Simple Insert Select Update and Delete in ASP. NET using My. SQL Database < /asp: Content> < asp: Content ID="Content. Content. Place. Holder. ID="head" runat="server"> < /asp: Content> < asp: Content ID="Content. Content. Place. Holder. ID="Main. Content" runat="server"> < table> < tr> < td class="td"> Name: < /td> < td> < asp: Text. Box ID="txt. Name" runat="server"> < /asp: Text. Box> < /td> < td> < asp: Label ID="lbl. SID" runat="server" Visible="false"> < /asp: Label> < /td> < /tr> < tr> < td class="td"> Address: < /td> < td> < asp: Text. Box ID="txt. Address" runat="server"> < /asp: Text. Box> < /td> < td> < /td> < /tr> < tr> < td class="td"> Mobile: < /td> < td> < asp: Text. Box ID="txt. Mobile" runat="server"> < /asp: Text. Box> < /td> < td> < /td> < /tr> < tr> < td class="td"> Email ID: < /td> < td> < asp: Text. Box ID="txt. Email" runat="server"> < /asp: Text. Box> < /td> < td> < /td> < /tr> < tr> < td> < /td> < td> < asp: Button ID="btn. Submit" runat="server" Text="Submit" On. Click="btn. Submit_Click" /> < asp: Button ID="btn. Update" runat="server" Text="Update" Visible="false" On. Click="btn. Update_Click" /> < asp: Button ID="btn. Cancel" runat="server" Text="Cancel" On. Click="btn. Cancel_Click" /> < /td> < td> < /td> < /tr> < /table> < div style="padding: 1. Total Student: < asp: Label ID="lbltotalcount" runat="server" Font- Bold="true"> < /asp: Label> < /p> < asp: Grid. View ID="Grid. View. Student" runat="server" Data. Key. Names="SID" On. Selected. Index. Changed="Grid. View. Student_Selected. Index. Changed" On. Row. Deleting="Grid. View. Student_Row. Deleting"> < Columns> < asp: Command. Field Header. Text="Update" Show. Select. Button="True" /> < asp: Command. Field Header. Text="Delete" Show. Delete. Button="True" /> < /Columns> < /asp: Grid. View> < /div> < /asp: Content> In the Web. Web. config < connection. Strings> < add name="Connection. String" connection. String="Server=localhost; userid=root; password=; Database=Testdb" provider. Name="My. Sql. Data. My. Sql. Client"/> < /connection. Strings> Now, in the code behind file “Student. Student. aspx. cs using System; using System. Collections. Generic; using System. Configuration; using System. Data; using System. Linq; using System. Web; using System. Web. UI; using System. Web. UI. Web. Controls; using My. Sql. Data. My. Sql. Client; namespace MYSQLCRUDApplication { public partial class Student : System. Web. UI. Page { #region My. Sql. Connection Connection and Page Lode My. Sql. Connection conn = new My. Sql. Connection(Configuration. Manager. Connection. Strings["Connection. String"]. Connection. String); protected void Page_Load(object sender, Event. Args e) { Try { if (! Page. Is. Post. Back) { Bind. Grid. View(); } } catch (Exception ex) { Show. Message(ex. Message); } } #endregion #region show message void Show. Message(string msg) { Client. Script. Register. Startup. Script(Page. Get. Type(), "validation", "< script language='javascript'> alert('" + msg + "'); < /script> "); } void clear() { txt. Name. Text = string. Empty; txt. Address. Text = string. Empty; txt. Mobile. Text = string. Empty; txt. Email. Text = string. Empty; txt. Name. Focus(); } #endregion #region bind data to Grid. View. Student private void Bind. Grid. View() { Try { if (conn. State == Connection. State. Closed) { conn. Open(); } My. Sql. Command cmd = new My. Sql. Command("Select * from Student ORDER BY SID DESC; ", conn); My. Sql. Data. Adapter adp = new My. Sql. Data. Adapter(cmd); Data. Set ds = new Data. Set(); adp. Fill(ds); Grid. View. Student. Data. Source = ds; Grid. View. Student. Data. Bind(); lbltotalcount. Text = Grid. View. Student. Rows. Count. To. String(); } catch (My. Sql. Exception ex) { Show. Message(ex. Message); } Finally { if (conn. State == Connection. State. Open) { conn. Close(); } } } #endregion #region Insert Data protected void btn. Submit_Click(object sender, Event. Args e) { Try { conn. Open(); My. Sql. Command cmd = new My. Sql. Command("Insert into student (Name,Address,Mobile,Email ) values (@Name,@Address,@Mobile,@Email)", conn); cmd. Parameters. Add. With. Value("@Name",txt. Name. Text); cmd. Parameters. Add. With. Value("@Address", txt. Address. Text); cmd. Parameters. Add. With. Value("@Mobile",txt.Mobile. Text); cmd.Parameters. Add. With.Value("@Email",txt.Email. Text); cmd.Execute. Non. Query(); cmd. Windows 7 Ultimate 32 Bit Auto Activation Cracked Trusted Sites . Dispose(); Show. Message("Registered successfully...!"); clear(); Bind. Grid. View(); } catch (My. Sql. Exception ex) { Show. Message(ex. Message); } Finally { conn. Close(); } } #endregion #region Selected. Index. Changed protected void Grid. View. Student_Selected. Index. Changed(object sender, Event. Args e) { Grid. View. Row row = Grid. View. Student. Selected. Row; lbl. SID. Text = row. Cells[2]. Text; txt. Name. Text = row. Cells[3]. Text; txt. Address. Text = row. Cells[4]. Text; txt. Email. Text = row.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
October 2017
Categories |