Questions tagged [executenonquery]

executes an SQL statement against the Connection object of a .NET Framework data provider, and returns the number of rows affected.

From MSDN: You can use the ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables), or to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements.

Although the ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.

243 questions
4
votes
1 answer

Problem with cmd.ExecuteNonQuery()

I am inserting values in to Database from a Webform using ADO.NET, C#. DB I am using is Oracle Database. Values are not being inserted and the program gets struck at the cmd.ExecuteNonquery() Here is my Code below, Please let me know If I am doing…
msbyuva
  • 3,467
  • 13
  • 63
  • 87
4
votes
1 answer

Cannot insert to database through console app

I am trying to write an insert method in a C# console application. I have set a breakpoint and it does hit ExecuteNonQuery, however, after stepping through it just hangs there and control does not seem to return back to the application. When I…
Johnathon64
  • 1,280
  • 1
  • 20
  • 45
4
votes
1 answer

Execute postgres script with npgsql

I have a script (*.sql) which creates tables. I am using Visual studio 2010 with npgsql to access postgres database. Could I execute a script from codebehind? This is the code I have tried: string sqlConnectionString = @"myconnection"; FileInfo…
Za7pi
  • 1,338
  • 7
  • 22
  • 33
4
votes
2 answers

oracle ExecuteNonQuery freezes on ASP.Net

I am trying to run a non query using a Oracle connection in ASP C# with CLR 4.5. Here is my code: string connectionString = ConfigurationManager.ConnectionStrings["OracleConnectionString1"].ConnectionString; OracleConnection conn = new…
max
  • 9,708
  • 15
  • 89
  • 144
4
votes
2 answers

How do I get the output from Database.ExecuteNonQuery?

I would like to use the Database.ExecuteNonQuery or something similar to execute a sql script and then capture the output. eg: xxx Table created My script: strArray = Regex.Split(streamReader.ReadToEnd(), "\r\nGO"); if…
AnilKumar
  • 121
  • 1
  • 3
  • 7
4
votes
2 answers

C# SQL Query - ExecuteNonQuery: Connection property has not been initialized

I have a number of blocks of code in my Windows Application that use the same structure to execute queries. After adding in a few new things to my code, these no longer work due to the error: "ExecuteNonQuery: Connection property has not been…
4
votes
5 answers

Getting C# To Insert Defaults Into SQL Server

How can I INSERT values into SQL Server that are stored in a string[] such that some of the values should be disregarded in favor of the values stored in SQL Server as default constraints on the table? What do I need to pass(e.g. NULL or something…
Ian Best
  • 510
  • 1
  • 11
  • 23
4
votes
4 answers

ExecutenonQuery not working

I have stored proc as below: ALTER PROC pr_Update_Users_Nomination ( @UserID AS VARCHAR(100), @Nominated AS BIT ) AS UPDATE User SET isNominated = @Nominated WHERE EMPID = @UserID; I want to call this procedure from c# code: Below…
BFry
  • 905
  • 2
  • 11
  • 24
3
votes
5 answers

c# ExecuteNonQuery always returns Zero

I think nothing's wrong with the connection because when I open it, it does not throw any error. So I guess the error is when I'm executing a command. This is my code: OleDbCommand cmd = new OleDbCommand("SELECT * FROM cars", conn); cmd.CommandType…
jovhenni19
  • 450
  • 2
  • 8
  • 24
3
votes
1 answer

C# MDF database is not inserting data even if success message is displayed

I need to insert data when user clicks. But, my code isn't doing it. Even though it displays the data inserted message, the data is not inserted. How can I find the mistake? private void bunifuFlatButton2_Click(object sender, EventArgs e) { …
user7883991
3
votes
2 answers

ExecuteNonQuery returning -1 when using sql COUNT despite the query string

For some reason, ExecuteNonQuery() in C# returns -1, though when I run a query separately, the value returns the actual value needed. For Example: try { var connString ="Data Source=ServerName;InitialCatalog=DatabaseName;Integrated…
JDavila
  • 409
  • 1
  • 7
  • 22
3
votes
2 answers

What is the maximum length of a SqlCommand query?

I am currently updating and reading values with a C# script from SQL Server 2014. When using a SQlCommand to executeNonQuery, it pops out an error when running the script: IndexOutOfRangeException: Array index is out of range. …
3
votes
1 answer

if a query has two update statements what will ExecuteNonQuery() return?

if a query has two update statements what executenonquery will return? Will it club the row count affected in both the update statements OR will return the count updated in last statement?
nectar
  • 9,525
  • 36
  • 78
  • 100
3
votes
1 answer

C# / ODP.Net: MERGE INTO - How to get numer of affected Rows?

I'm migrating a project from Mirosofts Oracle Provider (System.Data.OracleClient) to Oracles Data Provider ODP.Net (Oracle.DataAccess.Client), Version 11.2.0.4. The Problem I have right now is the following: When I'm running a MERGE INTO Statement…
3
votes
3 answers

Getting an error when calling stored procedure from C#

I am getting following error when calling a stored procedure in SQL Server from C#: Line 1: Incorrect syntax near 'spGet_Data'. Here is my code: public string GetData (string destinationFile) { string conectionString =…
user2138121
  • 65
  • 1
  • 6
1
2
3
16 17