Does not contain a definition for "Value"

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 1633 Views - Last Post: 06 June 2019 - 10:45 PM Rate Topic: -----

#1 c0pking   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 13-November 17

Does not contain a definition for "Value"

Posted 08 March 2019 - 05:30 PM

After compiling, it gives me the error: Does not contain a definition for "Value"
what should I use instead? i cant use .Text because I'm using the date format in my sql server for input

private void button1_Click(object sender, EventArgs e)
        {

            var conn = new SqlConnection("Data Source=MYNAMAJEOFF/SQLEXPRESS;initial catalog=OrderManegementSys;integrated security=true");
            var comm = new SqlCommand("Insert into CustomerInfo values ('" + txtCN.Text + "','" + txtcontact.Text + "','" + txtitem.Text + "','" + txtDOB.Value+ ")");



Is This A Good Question/Topic? 0
  • +

Replies To: Does not contain a definition for "Value"

#2 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Does not contain a definition for "Value"

Posted 08 March 2019 - 06:59 PM

Try this; I found it on Stack

private void button1_Click(object sender, EventArgs e)
        {
            string sqlFormattedDate = txtDOB.ToString("yyyy-MM-dd HH:mm:ss.fff");
            var conn = new SqlConnection("Data Source=MYNAMAJEOFF/SQLEXPRESS;initial catalog=OrderManegementSys;integrated security=true");
            var comm = new SqlCommand("Insert into CustomerInfo values ('" + txtCN.Text + "','" + txtcontact.Text + "','" + txtitem.Text + "','" + sqlFormattedDate + ")");
        }




https://stackoverflo...t-using-c-sharp

I can't test it because I don't have a SQL server setup. Let me know

This post has been edited by hexagod: 08 March 2019 - 07:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Does not contain a definition for "Value"

Posted 08 March 2019 - 07:19 PM

A few things to add.

First - when doing an INSERT you should label your columns, and correspond the values for them.

INSERT <table name> (col1, col2, ... coln)
VALUES (val1, val2, ... valn)

https://www.w3school.../sql_insert.asp

Second - you should be using parameters. ALWAYS use parameters. It helps deal with data type conversions as well as prevents sql injection.

Example:
cmd.Parameters.Add("@param1", SqlDbType.Int).Value = 1;
cmd.Parameters.Add("@param2", SqlDbType.DateTime).Value = DateTime.Now;


Lastly - you should be using 'using'. This helps dispose of objects when done and limits scope.
https://docs.microso...using-statement

Put it all together you have a descent snippet you can reuse time and time again.

            string sql = @"INSERT INTO myTable (col1, col2)
                        VALUES (@param1, @param2)";
            using (SqlConnection con = new SqlConnection("connection_string_info"))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    cmd.Parameters.Add("@param1", SqlDbType.Int).Value = 1;
                    cmd.Parameters.Add("@param2", SqlDbType.DateTime).Value = DateTime.Now;

                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                       Console.WriteLine(ex.Message);
                    }
                }
            }

Was This Post Helpful? 2
  • +
  • -

#4 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Does not contain a definition for "Value"

Posted 08 March 2019 - 07:58 PM

Modi is feeling generous tonight, throwing out some code B)

Good response Modi.
Was This Post Helpful? 0
  • +
  • -

#5 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Does not contain a definition for "Value"

Posted 08 March 2019 - 08:11 PM

Modi pwn3d me lol
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6969
  • View blog
  • Posts: 23,685
  • Joined: 05-May 12

Re: Does not contain a definition for "Value"

Posted 08 March 2019 - 08:11 PM

Some people decide to give up stuff for Lent, while others decide to be extra good/helpful/generous for Lent.

Or it could just be the new moon. :)
Was This Post Helpful? 0
  • +
  • -

#7 c0pking   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 13-November 17

Re: Does not contain a definition for "Value"

Posted 09 March 2019 - 06:33 AM

Thank you for the help, however, I'm getting another error which is on my connection string.

ar conn = new SqlConnection("Data Source=MYNAMAJEOFF\SQLEXPRESS;initial catalog=OrderManegementSys;integrated security=true");


the "\S"

it's reading as an escape sequence but my SQL server name: MYNAMAJEOFF\SQLEXPRESS

what should I do now?
the code isnt starting because of this

Posted Image
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 977
  • View blog
  • Posts: 3,850
  • Joined: 13-June 14

Re: Does not contain a definition for "Value"

Posted 09 March 2019 - 07:28 AM

You need to escape the '\' so that it's not treated as an escape sequence (i.e. write \\).
Was This Post Helpful? 0
  • +
  • -

#9 c0pking   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 13-November 17

Re: Does not contain a definition for "Value"

Posted 09 March 2019 - 10:17 AM

Alright

Now upon clicking that button while running the app, it gives an error in executenonquery


ERROR: ExecuteNonQuery: Connection property has not been initialized.




var conn = new SqlConnection("Data Source=MYNAMAJEOFF\\SQLEXPRESS;initial catalog=OrderManegementSys;integrated security=true");
            var comm = new SqlCommand("Insert into CustomerInfo values ('" + txtCN.Text + "','" + txtcontact.Text + "','" + txtitem.Text + "','" + txtDOB.Text + ")");
            
            conn.Open();
            comm.ExecuteNonQuery();  // here
            conn.Close();


Even after using hexagod's code guidance, it still gives the same error
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Does not contain a definition for "Value"

Posted 09 March 2019 - 10:18 AM

Per my example - your command object needs an open connection.

You also need parameters.
You also need to list off your columns in the order of insert.
Was This Post Helpful? 1
  • +
  • -

#11 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Does not contain a definition for "Value"

Posted 09 March 2019 - 10:24 AM

First off, you should be listening to modi not me. Secondly, you could try this?

string connectionString = "Data Source=MYNAMAJEOFF\\SQLEXPRESS;initial catalog=OrderManegementSys;integrated security=true");
var comm = new SqlCommand("Insert into CustomerInfo values ('" + txtCN.Text + "','" + txtcontact.Text + "','" + txtitem.Text + "','" + txtDOB.Text + ")");
            
     using (SqlConnection connection = new SqlConnection(connectionString))
       {
            comm.Connection.Open();
            
            comm.ExecuteNonQuery();  // here
            connection.Close();
       }


This post has been edited by hexagod: 09 March 2019 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Does not contain a definition for "Value"

Posted 09 March 2019 - 10:28 AM

Where abouts are you setting the command object's connection object?
04	     using (SqlConnection connection = new SqlConnection(connectionString))
05	       {
06	            comm.Connection.Open();
07	             
08	            comm.ExecuteNonQuery();  // here

Was This Post Helpful? 0
  • +
  • -

#13 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Does not contain a definition for "Value"

Posted 09 March 2019 - 10:30 AM

Sorry sql is new to me and my monitor is ridiculously tiny so making this harder. Not using my normal monitor so harder to put all this together for you. Fixed

string connectionString = "Data Source=MYNAMAJEOFF\\SQLEXPRESS;initial catalog=OrderManegementSys;integrated security=true");
string queryString = "Insert into CustomerInfo values ('" + txtCN.Text + "','" + txtcontact.Text + "','" + txtitem.Text + "','" + txtDOB.Text + ")";
            
     using (SqlConnection connection = new SqlConnection(connectionString))
       {
            SqlCommand comm = new SqlCommand(queryString, connection);
            comm.Connection.Open();
            
            comm.ExecuteNonQuery();  // here
            connection.Close();
       }


View Postmodi123_1, on 09 March 2019 - 10:28 AM, said:

Where abouts are you setting the command object's connection object?


fixed it... sorry I'm on a tiny monitor right so my indentation is all messed up.

This post has been edited by hexagod: 09 March 2019 - 10:31 AM

Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Does not contain a definition for "Value"

Posted 09 March 2019 - 10:32 AM

The point of the 'using' is you do not need to clean up your connection's closing... or the command object clean up.

01	string connectionString = "Data Source=MYNAMAJEOFF\\SQLEXPRESS;initial catalog=OrderManegementSys;integrated security=true");
02	string queryString = "Insert into CustomerInfo values ('" + txtCN.Text + "','" + txtcontact.Text + "','" + txtitem.Text + "','" + txtDOB.Text + ")";
03	             
04	     using (SqlConnection connection = new SqlConnection(connectionString))
05	       {
	            comm.Connection.Open();

06	            using( SqlCommand command = new SqlCommand(queryString, connection))
                    {
08	             
09	               comm.ExecuteNonQuery();  // here

                    }
11	       }

Was This Post Helpful? 0
  • +
  • -

#15 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Does not contain a definition for "Value"

Posted 09 March 2019 - 10:35 AM

ty modi =]
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2