Questions tagged [sqlcommand]

Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database which is used in Microsoft .NET.

SqlCommand is a class in the .NET Framework whose instances represent a SQL statement to later be executed against a SQL Server database.

General syntax:

SqlCommand is instantiated with a query string and a connection. In C#:

SqlCommand cmd = new SqlCommand("select CategoryName from Categories", con);

Reference

SqlCommand on MSDN

919 questions
-1
votes
1 answer

Syntax error (missing operator) in query expression in C# script task in SSIS

I get this error: System.Data.OleDb.OleDbException (0x80040E14): Syntax error (missing operator) in query expression ''O'Brien','O'Brien''. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at…
-1
votes
1 answer

Try to load images (categorized by city) from local DB to flowLayoutPanel

I'm trying to build a Form that can insert images to local DB and load them to flowLayoutPanel. Here is the layout of my form, I insert images in 'admin' tab (pic.1). In 'Browse Photos' tab I try to load photos of a specific city to flowLayoutPanel…
YS Chang
  • 1
  • 2
-1
votes
1 answer

SQLCommand Error on Visual Studio Designer (WinForms)

On a C# project (.NET Framework 4.8) when I try to create a control SqlCommand using the designer I get the following error Failed to create component 'SqlCommand'. The error message follows: 'System.invalidCastException: Unable to cast object of…
-1
votes
1 answer

SqlCommand with where clause backslashes

There is a matching record in the DB for DESKTOP-123\\markj. When I execute the code in SQL Server Management Studio, it returns one record. DECLARE @User NVARCHAR(260) = 'DESKTOP-123\\markj' SELECT r.RoleName FROM dbo.WindowsAuthenticationRole…
MaxPowers
  • 474
  • 1
  • 4
  • 18
-1
votes
1 answer

Why am I getting an exception telling me that I need to declare a scalar variable here (referencing a parameter in a query)?

I've got a parameterized query which I build up based on selection criteria the user chooses on a web page. For example, here is an example query string (completeQuery) that is built: SELECT M.MovieTitle, M.IMDBRating, M.MPAARating, M.YearReleased,…
-1
votes
2 answers

Create SqlParameter with name and type only (no value assigned)

I am having issue to use SqlClient to call some sql query with parameter declared but not assigned. The query I want to run is similar to below DECLARE @ID int; SELECT @ID = OrderId FROM Orders ORDER BY OrderId DESC; SELECT @ID; However, when I…
katrinawyh
  • 23
  • 3
-1
votes
1 answer

passing columnname with a parameter SQLcommand

I've been googling something I dont really cant understand. In short my problem is this; When using this; String sYear2 = "2020"; string query = @"Select decJan from Stats where intRecnum = (select intRecnum from Stats where intAr = @year)"; var cmd…
Tomas H
  • 57
  • 7
-1
votes
2 answers

I need help on IF EXIST ELSE

I need help on this line string query = @"IF EXISTS(Select * From u_Data Where TAG = '" + txtTag.Text + "') begin Update u_data set Name = '" + txtNama.Text + "', NIK = '" + txtNIK.Text + "', Department = '" + txtDept.Text + "' where…
-1
votes
2 answers

How to Refresh the Datagridview using c#.net

i am creating a simple crud in c#.net.after add the record record will show on the datagridview itself. but i couldn't do it. code which tried so far i attched below. code sql = "insert into…
-1
votes
2 answers

Object is null using SqlCommand stored procedure to Object

I am trying to execute a SQL stored procedure and collect the results in a List. I do not get errors, but the list is always null. The stored procedure produces over 100 records so I know that it should not be null, but I cannot figure out why it…
Rani Radcliff
  • 4,856
  • 5
  • 33
  • 60
-1
votes
2 answers

How to Divide Sql Column

I want to share the SQL table column. The first column will contain data upto 1-10, 11-20 in the second column and 21-30 in the 3rd column in the form of data.
-1
votes
1 answer

How do i resolve this procedure or function 'SelectFollow' expects parameter '@userid', which was not supplied.'?

I am trying to get the current userid from the parameter and to check if it exists in a Database Table using stored procedure public PartialViewResult AllStates(string state, string userid) { SqlCommand cmd = new SqlCommand(); Following…
Favour Emmanuel
  • 111
  • 3
  • 11
-1
votes
2 answers

'ExecuteNonQuery: Connection property has not been initialized.'

I have searched this forum, and have tried a multitude of possible solutions I found, but nothing is working. can anyone shed some light on this situation? Thanks! SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial…
Mr.B
  • 1
  • 6
-1
votes
1 answer

Login Comparing hash value in a database

I am attempting to make a simple sign on portion of an app I am creating. To confirm sign in, I am just attempting to make sure that the hash value of the password entered, matches that which is stored in my local database: App_Users ) '…
Jponder23
  • 27
  • 6
-1
votes
3 answers

The variable name '@ID' has already been declared. Variable names must be unique within a query batch or stored procedure

I have built a search function that will be searching by ID and by last name. This is my code so far: SqlCommand cmd = new SqlCommand("SELECT [ID],[Name],[LastName],[FirstName],[FinalGrade] FROM DATABASE WHERE ID = @ID OR LastName = @lname"); using…
User123
  • 27
  • 1
  • 1
  • 8