473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Urgent: I can't update database with SqlCommand

Sorry for the repeated post. I tried to update a record in database using SqlCommand.ExecuteNonQuery( ) method (I failed using SqlDataAdapter). I traced the above statement and found that it returned 1 rows afftected. Then I checked my database, the data was not updated at all

Can someone give me a clue ?
Nov 18 '05 #1
2 1364
can you trace it using SQL Profiler and run it in the query analyzer
directly.. and check.

Av.
"Tracey" <an*******@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
Sorry for the repeated post. I tried to update a record in database using
SqlCommand.ExecuteNonQuery( ) method (I failed using SqlDataAdapter). I
traced the above statement and found that it returned 1 rows afftected.
Then I checked my database, the data was not updated at all!

Can someone give me a clue ?

Nov 18 '05 #2
Tracey,

Are you using a sql string to pass command and values, or a stored
procedure?
Validate all your fields and double-check how you are attaching your
parameters.
There are different ways of achieving the same thing, but I use the approach
included below with stored procedures:-

<begin sample>

Private Sub ExampleSaveButtonRoutine...

Dim booCloseConn As Boolean = False

'Note: Connection string taken from the web.config file...
'sconOHA and objCommand are declared as Private page globals

If Me.sconOHA.State = ConnectionState.Closed Then
Me.sconOHA.ConnectionString =
ConfigurationSettings.AppSettings("DBConn")
Me.objCommand.Connection = Me.sconOHA
booCloseConn = True
End If
Me.objCommand.Parameters.Clear()
Me.objCommand.CommandType = CommandType.StoredProcedure
'Example comes from an edit form that can update as well as insert, so I
use the
'CommandArgument property to determine which stored procedure to use.

If Me.cmdEditSave.CommandArgument = "Insert" Then

Me.objCommand.CommandText = "usp_FC_FCPLog_INSERT"

'Insert the owner ID for the record
.Add(New SqlParameter("@PID", SqlDbType.Int)).Value =
CInt(Me.lblPID.Text)

Else

Me.objCommand.CommandText = "usp_FC_FCPLog_UPDATE"

'Attach the record ID
.Add(New SqlParameter("@FCPLID", SqlDbType.Int)).Value =
CInt(Me.lblFCPLID.Text)

End If

'Next, I validate the form and add the parameters...

With Me.objCommand.Parameters

'Do some validation (if required for your app)
Try
Me.lblErr.Visible = False
If (Me.txtMyValue1.Text <> "") And (Me.txtMyValue2.Text = "") Then
Throw New Exception
End If
Catch ex As Exception
Me.lblErr.Visible = True
Me.lblErr.Text = "## ~~ Error message to user goes here ~~ ##"
Exit Sub
End Try

'Validation criteria met, so add parameter values...

.Add(New SqlParameter("@MyFirstParam", SqlDbType.Int)).Value =
CType(Me.txtMyIntTextBox.Text, Integer)

If Me.txtFCDateOHPTRefApptOffered.Text <> "" Then
.Add(New SqlParameter("@MySecondParam", SqlDbType.DateTime)).Value =
Me.txtMyDateBox.xDate
End If

End With

'Having all the parameters validated and added, I connect and insert (or
update, etc...)

Try
Me.lblErr.Visible = False

If Me.sconOHA.State = ConnectionState.Closed Then
Me.sconOHA.Open()
End If
'#######################################

'And here I use the ExecuteNonQuery method to execute the sproc...
Me.objCommand.ExecuteNonQuery()

'#######################################

'Any other processing here, e.g. Rebind a datagrid to update display
(seperate routine)
Me.Bind_dgFC()
Catch ex As Exception
'Put SQL error or whatever on page for user feedback.
'Could add to server logs instead for example, depending on app
requirements.
Me.lblErr.Text = "Error saving/updating record: " & ex.Message.ToString
Me.lblErr.Visible = True

Finally
If booCloseConn Then
If Me.sconOHA.State = ConnectionState.Open Then
Me.sconOHA.Close()
End If
End If

Me.objCommand.Parameters.Clear()

End Try

End Sub

<end sample>
Hope that helps.
--
Alec MacLean
"Tracey" <an*******@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
Sorry for the repeated post. I tried to update a record in database using SqlCommand.ExecuteNonQuery( ) method (I failed using SqlDataAdapter). I
traced the above statement and found that it returned 1 rows afftected.
Then I checked my database, the data was not updated at all!
Can someone give me a clue ?

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DotNetJunkies User | last post by:
I am writing a distributed transaction code. My current scenario include a client database(Suppose client- having 4 main database) which can be installed anywhere which would connect to a public...
0
by: Nithin | last post by:
My code as an txt attachment. I have 2 drop down list boxes that on selection populate text boxes from my database table. I am able to display the correct values in these text boxes. I have 2...
2
by: Mike Hutton | last post by:
I have a rather odd problem. I have a SP which uses temp. tables along the way, and then returns a table of results: CREATE PROCEDURE dbo.usp_myproc( @pNameList VARCHAR(6000) ) AS
2
by: Xeijin | last post by:
URGENT I have an assignment to hand in tomorrow, I need to know how to perform numeric calculations in access, I dont know very much about databases so consider this a beginner's query! Well...
4
by: George | last post by:
Hi all, I am having trouble with updating my data in an Access database. here is my code: Imports System.Data.OleDb Dim AppPath As String = Mid(Application.ExecutablePath, 1,...
1
by: artteam | last post by:
private void btnSave_Click(object sender, EventArgs e) { //this.dataGridView1.CurrentRow.Cells.Value.ToString()) string sqlCommand; sqlCommand = ...
12
by: Simon | last post by:
Hi all, I'm having a baffling problem with a windows service that I'm working on. Basically, I am using a typed dataset to insert a large number of rows into an SQL Server 2005 database. But...
0
by: halex | last post by:
Hello, I am having deadlock problem when I have a lot of visitors on my website at the same time. I am using NetTiers templates to generate C# classes for accessing DB layer and problem is in my...
1
by: iswar | last post by:
Hi friends.. Im trying to bind a value from dropdownlist placed in gird view to another cell. i want to update a database field with the selected value from dropdownlist and which must be bind to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.