Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
AnswerRe: Virtual Function Pin
Scott Dorman20-Sep-07 5:10
professionalScott Dorman20-Sep-07 5:10 
QuestionLicense Key Generation Pin
M. J. Jaya Chitra17-Sep-07 18:56
M. J. Jaya Chitra17-Sep-07 18:56 
AnswerRe: License Key Generation Pin
Harkamal Singh18-Sep-07 0:42
Harkamal Singh18-Sep-07 0:42 
GeneralRe: License Key Generation Pin
M. J. Jaya Chitra18-Sep-07 2:20
M. J. Jaya Chitra18-Sep-07 2:20 
GeneralRe: License Key Generation Pin
#realJSOP17-Jul-08 3:14
mve#realJSOP17-Jul-08 3:14 
AnswerRe: License Key Generation Tool Pin
RWBitters27-Nov-09 15:11
RWBitters27-Nov-09 15:11 
QuestionDatabase retrieving problem Pin
Nishad8517-Sep-07 18:12
Nishad8517-Sep-07 18:12 
QuestionProblem with SQL Update using Variables in C# [modified] Pin
ba1959nh17-Sep-07 16:29
ba1959nh17-Sep-07 16:29 
I am trying to update a record using 2 variables in my UPDATE statement in C#. The Statement is executed using ExecuteNonQuery() which returns 0 - correctly indicating that 0 records were updated - but I can't figure out why. I have included source code and log data that I collected.

I appreciate any help in exposing my screw-up.
Wink | ;-)
Bill



//*****************************************
// BEGIN OF CODE STUB
//*****************************************

// FYI:
// myConn is a Global of type OleDbConnection
// and gets opened in the
// fnGetConnectedToDatabase() call


int nCustID = -1;
int nRouteID = -1;


//**************************************************************
// Get the RouteID for the selected RouteName
//**************************************************************

// Find the index in the ComboBox
// Look up the associated RouteID in listBoxRouteIDs
int idx = comboBoxRoutes.SelectedIndex;

if (idx != -1)
{
nRouteID = Convert.ToInt32(listBoxRouteIDs.Items[idx]);
foreach (string sCustName in listBoxToAddToRoute.Items)
{
CommonSubsClass.WriteToLog("Open Connection"); // DEBUG
fnGetConnectedToDatabase();

// THIS CODE WORKS FINE IN LOOKING UP THE CUSTOMER ID
// 1. Find out the Customer ID for this customer
// 2. Update the CustomerInfo record
string selectSQL = "SELECT CustomerInfo.* FROM CustomerInfo WHERE CustomerInfo.Customer = ?";


myDataAdapter = new OleDbDataAdapter(selectSQL, myConn);

OleDbCommand selectCMD = new OleDbCommand(selectSQL, myConn);
myDataAdapter.SelectCommand = selectCMD;

// Add parameters and set values.
selectCMD.Parameters.Add("@CustName", OleDbType.VarChar, 40).Value = sCustName;

// Instantiate a DataSet
DataSet CustomerDataset = new DataSet();

// Populate the data table "CustomerInfo"
CustomerDataset.Clear();
myDataAdapter.Fill(CustomerDataset,"CustomerInfo");

foreach (DataRow myRow in CustomerDataset.Tables["CustomerInfo"].Rows)
{

// Need to handle Null values
try
{
if (myRow["cust_id"] != DBNull.Value)
{
nCustID =(int) myRow["cust_id"];
}

}
catch (InvalidCastException ex)
{
MessageBox.Show("Error in InvalidCastException...\n" + ex.Message + "\n" + ex.StackTrace);
}


}

CommonSubsClass.WriteToLog("Close Connection [1]"); // DEBUG
myConn.Close();

CommonSubsClass.WriteToLog("Open Connection [2]"); // DEBUG
fnGetConnectedToDatabase();

//************************************************
// I BELIEVE THE PROBLEM MUST BE BELOW HERE !!!
// NOTES:
// I HAVE CONFIRMED TABLE AND FIELD NAMES
//************************************************
string updateSQL = "UPDATE CustomerInfo SET RouteID = @RouteID WHERE cust_id = @cust_id";

OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommand cmd;

// Create the UpdateCommand.
cmd = new OleDbCommand(updateSQL, myConn);

cmd.Parameters.Add("@cust_id", OleDbType.Integer).Value = nCustID;

// V2.2.5 - 04-JAN-2007
// Added RouteID to CustomerInfo table
cmd.Parameters.Add("@RouteID", OleDbType.Integer).Value = nRouteID;
CommonSubsClass.WriteToLog(updateSQL + " [" + nCustID.ToString() + "] [" + nRouteID + "]");

try
{
int retval = -1;

retval = cmd.ExecuteNonQuery();

string sCustomerInfoChanged = "";
sCustomerInfoChanged = "Customer Route Changed: [CUST ID:" + nCustID.ToString() + "] to " + nRouteID;
CommonSubsClass.WriteToLog(sCustomerInfoChanged);
CommonSubsClass.WriteToLog("cmd.ExecuteNonQuery() Returned: " + retval.ToString());

}
catch (Exception ex)
{
MessageBox.Show("DB Update Exception...\n" + ex.Message + "\n" + ex.StackTrace);
}
//*****************************************
// END OF CODE STUB
//*****************************************

//************************************************************
HERE ARE MY LOG ENTRIES (CommonSubsClass.WriteToLog() CALLS:
9/17/2007 10:00:54 PM: Open Connection
9/17/2007 10:00:54 PM: Close Connection [1]
9/17/2007 10:00:54 PM: Open Connection [2]
9/17/2007 10:01:26 PM: UPDATE CustomerInfo SET RouteID = @RouteID WHERE cust_id = @cust_id [187] [4]
9/17/2007 10:01:36 PM: Customer Route Changed: [CUST ID:187] to 4
9/17/2007 10:01:55 PM: cmd.ExecuteNonQuery() Returned: 0
9/17/2007 10:01:55 PM: Close Connection [2]
//*************************************************************


-- modified at 22:50 Monday 17th September, 2007
AnswerRe: Problem with SQL Update using Variables in C# Pin
PIEBALDconsult17-Sep-07 18:34
mvePIEBALDconsult17-Sep-07 18:34 
AnswerRe: Problem with SQL Update using Variables in C# Pin
ba1959nh18-Sep-07 2:58
ba1959nh18-Sep-07 2:58 
GeneralRe: Problem with SQL Update using Variables in C# Pin
PIEBALDconsult18-Sep-07 5:03
mvePIEBALDconsult18-Sep-07 5:03 
QuestionQuestion on checkedlistbox if statement Pin
falles0117-Sep-07 15:27
falles0117-Sep-07 15:27 
QuestionLogon with UserName and Password Pin
T4AMD17-Sep-07 12:15
T4AMD17-Sep-07 12:15 
AnswerRe: Logon with UserName and Password Pin
ChrisKo17-Sep-07 12:33
ChrisKo17-Sep-07 12:33 
QuestionRe: Logon with UserName and Password Pin
T4AMD17-Sep-07 12:42
T4AMD17-Sep-07 12:42 
AnswerRe: Logon with UserName and Password Pin
ChrisKo17-Sep-07 12:50
ChrisKo17-Sep-07 12:50 
AnswerRe: Logon with UserName and Password Pin
PIEBALDconsult17-Sep-07 14:01
mvePIEBALDconsult17-Sep-07 14:01 
QuestionConsole application keypress Pin
ballzinator17-Sep-07 11:50
ballzinator17-Sep-07 11:50 
AnswerRe: Console application keypress Pin
ChrisKo17-Sep-07 12:27
ChrisKo17-Sep-07 12:27 
GeneralRe: Console application keypress Pin
ballzinator18-Sep-07 3:27
ballzinator18-Sep-07 3:27 
GeneralRe: Console application keypress Pin
Big Daddy Farang18-Sep-07 8:22
Big Daddy Farang18-Sep-07 8:22 
Questionwhat is better c# or vb.net ?? Pin
sofy200817-Sep-07 11:49
sofy200817-Sep-07 11:49 
AnswerRe: what is better c# or vb.net ?? Pin
Matthew Cuba17-Sep-07 12:42
Matthew Cuba17-Sep-07 12:42 
AnswerRe: what is better c# or vb.net ?? Pin
Patrick Etc.17-Sep-07 13:33
Patrick Etc.17-Sep-07 13:33 
GeneralRe: what is better c# or vb.net ?? Pin
PIEBALDconsult17-Sep-07 13:58
mvePIEBALDconsult17-Sep-07 13:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.