ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Insert Update using ASP.Net SqlDataSource control parameters in C#<p>Hi&nbsp;dorsa,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;gvEmployees&#34; runat=&#34;server&#34; Caption=&#34;کارمندان موجود در پایگاه داده&#34; DataSourceID=&#34;sqlDtSrcEmployees&#34; AutoGenerateColumns=&#34;False&#34; OnRowDataBound=&#34;gvEmployees_RowDataBound&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Emp_No&#34; HeaderText=&#34;EmployeeID&#34; SortExpression=&#34;EmployeeID&#34; /&gt; &lt;asp:BoundField DataField=&#34;Emp_Name&#34; HeaderText=&#34;Name&#34; SortExpression=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;Emp_JoiningDate&#34; HeaderText=&#34;HireDate&#34; SortExpression=&#34;HireDate&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:SqlDataSource ID=&#34;sqlDtSrcEmployees&#34; runat=&#34;server&#34; ConnectionString=&#34;&lt;%$ ConnectionStrings:NorthwindConnectionString2 %&gt;&#34; InsertCommand=&#34;INSERT INTO [Employee] ([Emp_No],[Emp_Name],[Emp_JoiningDate]) VALUES (@id,@Name, @HireDate)&#34; SelectCommand=&#34;SELECT Emp_No,Emp_Number,Emp_Name,Emp_JoiningDate,Emp_ResignDate,Emp_Status,Emp_CurrentDesignation,Emp_CurrentGross FROM [Employee]&#34; UpdateCommand=&#34;Update Employee SET [Emp_Name]=@Name, [Emp_JoiningDate]=@HireDate WHERE Emp_No=@id&#34; OnInserting=&#34;sqlDtSrcEmployees_Inserting&#34; OnUpdating=&#34;sqlDtSrcUpdate_Updating&#34;&gt; &lt;InsertParameters&gt; &lt;asp:Parameter Name=&#34;id&#34; Type=&#34;Int32&#34; /&gt; &lt;asp:Parameter Name=&#34;HireDate&#34; Type=&#34;DateTime&#34; /&gt; &lt;asp:ControlParameter ControlID=&#34;txtName&#34; Name=&#34;Name&#34; PropertyName=&#34;Text&#34; Type=&#34;String&#34; /&gt; &lt;/InsertParameters&gt; &lt;UpdateParameters&gt; &lt;asp:Parameter Name=&#34;id&#34; Type=&#34;Int32&#34; /&gt; &lt;asp:ControlParameter ControlID=&#34;txtName&#34; Name=&#34;Name&#34; PropertyName=&#34;Text&#34; Type=&#34;String&#34; /&gt; &lt;asp:Parameter Name=&#34;HireDate&#34; Type=&#34;DateTime&#34; /&gt; &lt;/UpdateParameters&gt; &lt;/asp:SqlDataSource&gt; Id:&lt;asp:TextBox runat=&#34;server&#34; ID=&#34;TextBox1&#34; /&gt; &lt;br /&gt; Name:&lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtName&#34; /&gt; &lt;asp:Button Text=&#34;Insert&#34; runat=&#34;server&#34; OnClick=&#34;Button1_Click1&#34; /&gt; &lt;asp:Button ID=&#34;Button1&#34; Text=&#34;Update&#34; runat=&#34;server&#34; OnClick=&#34;Button2_Click&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">C#</span></strong></p> <pre class="brush: csharp">protected void gvEmployees_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType == DataControlRowType.DataRow) { DateTime usDate = (DateTime)DataBinder.Eval(e.Row.DataItem, &#34;Emp_JoiningDate&#34;); System.Globalization.PersianCalendar faDate = new System.Globalization.PersianCalendar(); string day = faDate.GetDayOfMonth(usDate).ToString(&#34;D2&#34;); string month = faDate.GetMonth(usDate).ToString(&#34;D2&#34;); string year = faDate.GetYear(usDate).ToString(&#34;D4&#34;); e.Row.Cells[2].Text = string.Format(&#34;{0} / {1} / {2}&#34;, year, month, day); } } catch { } } protected void sqlDtSrcEmployees_Inserting(object sender, SqlDataSourceCommandEventArgs e) { try { int day = Convert.ToInt32(DateTime.Now.Day); int month = Convert.ToInt32(DateTime.Now.Month); int year = Convert.ToInt32(DateTime.Now.Year); System.Globalization.PersianCalendar faDate = new System.Globalization.PersianCalendar(); e.Command.Parameters[&#34;@HireDate&#34;].Value = faDate.ToDateTime(year, month, day, 23, 0, 0, 0); e.Command.Parameters[&#34;@id&#34;].Value = Int32.Parse(TextBox1.Text); } catch { } } protected void Button1_Click1(object sender, EventArgs e) { sqlDtSrcEmployees.Insert(); } protected void sqlDtSrcUpdate_Updating(object sender, SqlDataSourceCommandEventArgs e) { try { int day = Convert.ToInt32(DateTime.Now.Day); int month = Convert.ToInt32(DateTime.Now.Month); int year = Convert.ToInt32(DateTime.Now.Year); System.Globalization.PersianCalendar faDate = new System.Globalization.PersianCalendar(); e.Command.Parameters[&#34;@HireDate&#34;].Value = faDate.ToDateTime(year, month, day, 23, 0, 0, 0); e.Command.Parameters[&#34;@id&#34;].Value = Int32.Parse(TextBox1.Text); } catch { } } protected void Button2_Click(object sender, EventArgs e) { sqlDtSrcEmployees.Update(); }</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/QytdNxI.gif" alt="" width="367" height="235" /></span></strong></p>https://www.aspforums.net:443/Threads/133216/Insert-Update-using-ASPNet-SqlDataSource-control-parameters-in-C/https://www.aspforums.net:443/Threads/133216/Insert-Update-using-ASPNet-SqlDataSource-control-parameters-in-C/Tue, 11 Dec 2018 04:32:55 GMT