ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2017 www.aspforums.com. All rights reserved.Add record to Database if Button text is Add and Delete record form Database if Button text is Added using C# in ASP.Net<p>Hi <a class="username" rel="micah"> micah</a>,</p> <p>Please explain in more detail.</p>http://www.aspforums.net/Threads/141357/Add-record-to-Database-if-Button-text-is-Add-and-Delete-record-form-Database-if-Button-text-is-Added-using-C-in-ASPNet/http://www.aspforums.net/Threads/141357/Add-record-to-Database-if-Button-text-is-Add-and-Delete-record-form-Database-if-Button-text-is-Added-using-C-in-ASPNet/Mon, 09 Jan 2017 02:53:53 GMTAdd record to Database if Button text is Add and Delete record form Database if Button text is Added using C# in ASP.Net<p>the code above was provided by you at the link i displayed, here is what i want to modifer from that code. After clicking ADD button the recode will be added and ADDED button will display. but i want the ADDED button to delet the record be clicking ADDED button. So that the button ADD will be for inserting record while ADDED button will be for deleting the record.</p>http://www.aspforums.net/Threads/141357/Add-record-to-Database-if-Button-text-is-Add-and-Delete-record-form-Database-if-Button-text-is-Added-using-C-in-ASPNet/http://www.aspforums.net/Threads/141357/Add-record-to-Database-if-Button-text-is-Add-and-Delete-record-form-Database-if-Button-text-is-Added-using-C-in-ASPNet/Mon, 09 Jan 2017 03:31:00 GMTAdd record to Database if Button text is Add and Delete record form Database if Button text is Added using C# in ASP.Net<p>Hi <a class="username" rel="micah">micah</a>,</p> <p>I have created sample code which full-fill your requirement.</p> <p><span style="text-decoration: underline;">HTML</span></p> <p><span style="text-decoration: underline;">LoginPage</span></p> <pre class="brush: html">&lt;div&gt; &lt;table border=&#34;0&#34; cellpadding=&#34;0&#34; cellspacing=&#34;0&#34;&gt; &lt;tr&gt; &lt;td&gt; UserName &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txtUserName&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Password &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txtPassword&#34; TextMode=&#34;Password&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan=&#34;2&#34;&gt; &lt;asp:Button ID=&#34;btnSubmit&#34; Text=&#34;Login&#34; OnClick=&#34;OnLogin&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">private string constring = ConfigurationManager.ConnectionStrings[&#34;constring&#34;].ToString(); protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { } } protected void OnLogin(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(constring)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM [User] WHERE UserName=@UserName AND Password=@Password&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@Username&#34;, txtUserName.Text.Trim()); cmd.Parameters.AddWithValue(&#34;@Password&#34;, txtPassword.Text.Trim()); con.Open(); SqlDataReader idr = cmd.ExecuteReader(); if (idr.Read()) { Session[&#34;UserId&#34;] = idr[&#34;Id&#34;].ToString(); Response.Redirect(&#34;Default2.aspx&#34;); } con.Close(); } } }</pre> <p><span style="text-decoration: underline;">HTML</span></p> <p>Default2.aspx Page</p> <pre class="brush: html">&lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;div&gt; &lt;asp:DataList ID=&#34;dlBooks&#34; runat=&#34;server&#34; OnItemDataBound=&#34;dlBooks_ItemDataBound&#34;&gt; &lt;HeaderTemplate&gt; &lt;table class=&#34;table&#34; border=&#34;1&#34;&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:Label Text=&#34;Book Pics&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;Label1&#34; Text=&#34;BookName&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;Label2&#34; Text=&#34;Favorite Book&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:Label ID=&#34;FavoriteId&#34; Text=&#34;0&#34; Visible=&#34;false&#34; runat=&#34;server&#34; /&gt; &lt;asp:Label ID=&#34;lblId&#34; Text=&#39;&lt;%#Eval(&#34;Id&#34;) %&gt;&#39; Visible=&#34;false&#34; runat=&#34;server&#34; /&gt; &lt;asp:Image ID=&#34;Imagebooks&#34; ImageUrl=&#39;&lt;%# &#34;Images/&#34; + Eval(&#34;Name&#34;) %&gt;&#39; Width=&#34;80px&#34; Height=&#34;50px&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblbookName&#34; Text=&#39;&lt;%#Eval(&#34;Name&#34;)%&gt;&#39; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:Button ID=&#34;btnAdd&#34; Text=&#34;Add&#34; OnClick=&#34;OnfavoriteBook&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;/table&gt; &lt;/FooterTemplate&gt; &lt;/asp:DataList&gt; &lt;/div&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:LinkButton Text=&#34;Logout&#34; OnClick=&#34;OnLogOut&#34; runat=&#34;server&#34; /&gt; &lt;/form&gt;</pre> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">private string constring = ConfigurationManager.ConnectionStrings[&#34;constring&#34;].ToString(); protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.Populatebooks(); } } private void Populatebooks() { string userId = Session[&#34;UserId&#34;].ToString(); using (SqlConnection con = new SqlConnection(constring)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM [dbo].[Files]&#34;, con)) { DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); dlBooks.DataSource = dt; dlBooks.DataBind(); } } } protected void OnfavoriteBook(object sender, EventArgs e) { DataListItem item = (sender as Button).NamingContainer as DataListItem; int bookId = Convert.ToInt32((item.FindControl(&#34;lblId&#34;) as Label).Text); int favoriteId = Convert.ToInt32((item.FindControl(&#34;FavoriteId&#34;) as Label).Text); using (SqlConnection con = new SqlConnection(constring)) { if ((item.FindControl(&#34;btnAdd&#34;) as Button).Text.ToUpper() == &#34;ADD&#34;) { using (SqlCommand cmd = new SqlCommand(&#34;INSERT INTO Dim_favorite values(@UserId,@BookId)&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@UserId&#34;, Session[&#34;UserId&#34;]); cmd.Parameters.AddWithValue(&#34;@BookId&#34;, bookId); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } else { using (SqlCommand cmd = new SqlCommand(&#34;DELETE FROM Dim_favorite WHERE FavoriteId=@FavoriteId&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@FavoriteId&#34;, favoriteId); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } this.Populatebooks(); } protected void dlBooks_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { string bookid = (e.Item.FindControl(&#34;lblId&#34;) as Label).Text; using (SqlConnection con = new SqlConnection(constring)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT f.Id AS TestId,fav.FavoriteId,f.Name,&#39;Added&#39; [Status] FROM Files f INNER JOIN Dim_favorite fav On f.Id = fav.BookId WHERE fav.UserId=&#39;&#34; + Session[&#34;UserId&#34;] + &#34;&#39; &#34;, con)) { DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); foreach (DataRow row in dt.Rows) { if (bookid == row[&#34;TestId&#34;].ToString()) { (e.Item.FindControl(&#34;btnAdd&#34;) as Button).Text = &#34;Added&#34;; (e.Item.FindControl(&#34;FavoriteId&#34;) as Label).Text = row[&#34;FavoriteId&#34;].ToString(); } } } } } } protected void OnLogOut(object sender, EventArgs e) { Response.Redirect(&#34;Login.aspx&#34;); }</pre> <p><span style="text-decoration: underline;">Screenshot</span></p> <p><span style="text-decoration: underline;"><img src="http://i988.photobucket.com/albums/af3/mudassarkhan/141357.gif" alt="" width="472" height="359" /></span></p>http://www.aspforums.net/Threads/141357/Add-record-to-Database-if-Button-text-is-Add-and-Delete-record-form-Database-if-Button-text-is-Added-using-C-in-ASPNet/http://www.aspforums.net/Threads/141357/Add-record-to-Database-if-Button-text-is-Add-and-Delete-record-form-Database-if-Button-text-is-Added-using-C-in-ASPNet/Mon, 09 Jan 2017 04:42:24 GMT