Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 20th, 2006, 09:05 AM
Registered User
Join Date: Dec 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# Sql dynamic update


I am trying to incorporate a function in a contact list that i've made whereby when the user wishes to update someone's details they only have to fill in textbox controls or select from lists and then press 'ONE BUTTON'. what i want to happen when people press that button is for the program to update only the fields that contain values. So if there are 3 textboxes, for instance, and they are for address, name, email. if someone only needs to update address they fill in the address box and press 'UPDATE'. I would only want the addrress field updating but what happens is it updates everything, inlcuding the blank fields.

Now this shouldn't happen because i don't release their parameters UNLESS the controls contain values. ***Please someone tell me if i'm wrong in my method there***

Please look over the code and explain for me?

string updateup =

"UPDATE ContactList SET Address = '" +comboBoxDepartmentUpdate.Text+ "' " +
", FullName = '" +txtNameUpdate.Text+ "' "+
", DirectLine = '" +txtDirectUpdate.Text+ "' "+
", Mobile = '" +txtMobileUpdate.Text+ "' "+

"WHERE Address = '" +comboBoxDepartment.Text+ "' "+
"AND FullName = '" +comboBoxName.Text+ "'"

sqlInsertCommand1.CommandType = CommandType.Text;
sqlInsertCommand1.CommandText = updateup;
sqlInsertCommand1.Connection = conn3;

if(comboBoxDepartment.SelectedIndex >= 0)
sqlInsertCommand1.Parameters.Add("@Address", SqlDbType.Char, 255, "Address").Value = comboBoxDepartmentUpdate.Text;

if(txtNameUpdate.TextLength > 0)
sqlInsertCommand1.Parameters.Add("@FullName", SqlDbType.Char, 255, "FullName").Value = txtNameUpdate.Text;

if(txtDirectUpdate.TextLength > 0)
sqlInsertCommand1.Parameters.Add("@DirectLine", SqlDbType.Char, 255, "DirectLine").Value = txtDirectUpdate.Text;

if(txtMobileUpdate.TextLength > 0)
sqlInsertCommand1.Parameters.Add("@Mobile", SqlDbType.Char, 255, "Mobile").Value = txtMobileUpdate.Text;

The RunQuery is the function that executes the query via the SqlInsertCommand.

I hope i've explain properly. It seems that there's no way of contraining what's executed other than writing a sqlStatement for each case, which would be REALLY annoying if you had 30 controls that needed updating.

Any help would be much appreciated.


When **** becomes valuable the poor will be born without *******s.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot Update the vals in a Dynamic DetailsView Itech ASP.NET 2.0 Professional 6 April 2nd, 2008 01:59 AM
Dynamic List in UPDATE page neolynx Classic ASP Databases 1 November 21st, 2007 03:22 AM
Dynamic Image Update aymawadi ADO.NET 1 April 10th, 2005 04:53 PM
Dynamic SQL Tina Pro VB.NET 2002/2003 0 April 25th, 2004 07:26 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.