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.Update specific column in database based on user input using C# in ASP.Net<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Take reference to below code change yor code.</p> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <pre class="brush: csharp">protected void btn_update_Click(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; int id = int.Parse(lbid.Text); &nbsp;&nbsp;&nbsp; string filePath = FileUpload1.PostedFile.FileName; &nbsp;&nbsp;&nbsp; string customername = txtcus.Text; &nbsp;&nbsp;&nbsp; string filename = Path.GetFileName(filePath); &nbsp;&nbsp;&nbsp; string ext = Path.GetExtension(filename); &nbsp;&nbsp;&nbsp; string contenttype = String.Empty; &nbsp;&nbsp;&nbsp; switch (ext) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case &#34;.doc&#34;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contenttype = &#34;application/vnd.ms-word&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case &#34;.docx&#34;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contenttype = &#34;application/vnd.ms-word&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case &#34;.xls&#34;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contenttype = &#34;application/vnd.ms-excel&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case &#34;.xlsx&#34;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contenttype = &#34;application/vnd.ms-excel&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case &#34;.jpg&#34;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contenttype = &#34;image/jpg&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case &#34;.png&#34;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contenttype = &#34;image/png&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case &#34;.gif&#34;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contenttype = &#34;image/gif&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case &#34;.pdf&#34;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contenttype = &#34;application/pdf&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; Stream fs = FileUpload1.PostedFile.InputStream; &nbsp;&nbsp;&nbsp; BinaryReader br = new BinaryReader(fs); &nbsp;&nbsp;&nbsp; Byte[] bytes = br.ReadBytes((Int32)fs.Length); &nbsp;&nbsp;&nbsp; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; &nbsp;&nbsp;&nbsp; SqlConnection con = new SqlConnection(constr); &nbsp;&nbsp;&nbsp; string query = &#34;UPDATE Customer SET &#34;; &nbsp;&nbsp;&nbsp; SqlCommand cmd = new SqlCommand(query, con); &nbsp;&nbsp;&nbsp; if (!string.IsNullOrEmpty(customername)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; query += &#34;CustomerName =@CustomerName,&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@CustomerName&#34;, customername); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; if (!string.IsNullOrEmpty(filename)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; query += &#34;Name = @Name,&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.Add(&#34;@Name&#34;, SqlDbType.VarChar).Value = filename; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; if (bytes.Length &gt; 0) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; query += &#34;Data=@Data,&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.Add(&#34;@Data&#34;, SqlDbType.Binary).Value = bytes; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; if (!string.IsNullOrEmpty(contenttype)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; query += &#34;ContentType=@ContentType,&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.Add(&#34;@ContentType&#34;, SqlDbType.VarChar).Value = contenttype; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; query = query.TrimEnd(&#39;,&#39;); &nbsp;&nbsp;&nbsp; query += &#34; WHERE CustomerId =@Id&#34;; &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@Id&#34;, id); &nbsp;&nbsp;&nbsp; con.Open(); &nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp; con.Close(); }</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/186069/Update-specific-column-in-database-based-on-user-input-using-C-in-ASPNet/https://www.aspforums.net:443/Threads/186069/Update-specific-column-in-database-based-on-user-input-using-C-in-ASPNet/Wed, 28 Aug 2019 03:28:01 GMT