Does not contain a definition for "Value"

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 - 12:20 PM

gives an error cus command does not exist in this context



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))
	          {
	            
                comm.Connection.Open(); // here 
                
                using( SqlCommand command = new SqlCommand(queryString, connection))
                    {
	             
	               command.ExecuteNonQuery();  // here

                   connection.Close();


                    }
	       }

Was This Post Helpful? 0
  • +
  • -

#17 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 - 12:37 PM

Looking online,

using System.Data;

have you imported System.Data ?
Was This Post Helpful? 0
  • +
  • -

#18 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 - 12:53 PM

yea its there

Anyways!

The CODE is WORKING
Thank you so much both of you.

I did a mistake, im really sorry it was from my side.

I was using the name of a label "Customer Date" instead of using the datetimepicker tool name I set


Extremely sorry for causing this mess but I'll rep both of you for helping me out!
Was This Post Helpful? 0
  • +
  • -

#19 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 - 12:59 PM

No worries, I learned some things too. =]
Was This Post Helpful? 0
  • +
  • -

#20 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 - 01:10 PM

View Postmodi123_1, on 09 March 2019 - 02:19 AM, said:

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


This is a very important piece of advice. c0pking, please, please heed this and don't concatenate strings to form your SQL statements. You should educate yourself on SQL injection to understand why that's bad.
Was This Post Helpful? 1
  • +
  • -

#21 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 - 01:25 PM

I believe the user can insert SQL commands into the controls to do things they shouldn't be able to, if you're not using parameters. So yeah, definitely take modi's/ndc advice.
Was This Post Helpful? 0
  • +
  • -

#22 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 - 01:45 PM

View Postndc85430, on 09 March 2019 - 01:10 PM, said:

View Postmodi123_1, on 09 March 2019 - 02:19 AM, said:

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


This is a very important piece of advice. c0pking, please, please heed this and don't concatenate strings to form your SQL statements. You should educate yourself on SQL injection to understand why that's bad.



Yes, I will do that.

Thank you for the piece of advice.
Was This Post Helpful? 0
  • +
  • -

#23 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1399
  • View blog
  • Posts: 1,989
  • Joined: 24-August 11

Re: Does not contain a definition for "Value"

Posted 11 March 2019 - 01:56 PM

Here's a great tutorial from our own Curtis Rutland on the topic of parameterizing queries.

This post has been edited by h4nnib4l: 11 March 2019 - 01:56 PM

Was This Post Helpful? 1
  • +
  • -

#24 RushabhVerma   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 13
  • Joined: 23-April 19

Re: Does not contain a definition for "Value"

Posted 12 May 2019 - 11:43 PM

Try This:

var comm = new SqlCommand("Insert into CustomerInfo values ('" + txtCN.Text + "','" + txtcontact.Text + "','" + txtitem.Text + "','" + txtDOB.Value.ToString+ ")");
Was This Post Helpful? -2
  • +
  • -

#25 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,100
  • Joined: 12-December 12

Re: Does not contain a definition for "Value"

Posted 13 May 2019 - 12:06 AM

RushabhVerma If you read the entire thread you will see that the importance of using parameters with SQL statements has been stressed, so your example that does not use parameters is a backward step.

Your example is also an invalid statement. In fact, you have just copied it from an earlier post, with the error.
Was This Post Helpful? 0
  • +
  • -

#26 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 13 May 2019 - 04:13 PM

All the links you will need are likely in my signature for this example. Simply fixing this would be to do the following ::
                            using  (SqlCommand cmd = new SqlCommand("Insert into CustomerInfo values (@txtCN_PH,@txtContact_PH,@txtItem_PH,@txtDOB)", Con))
                            {   //Use using blocks for they are self disposing when done using
                                //The below parameters takes the parameter name, and then sets the value of that parameter
                                //So for the first parameter, we are setting the parameter "txtCN_PH" to the value of Values[0] and then so on for each.
                                cmd.Parameters.AddWithValue("@txtCN_PH", txtCN.Text);
                                cmd.Parameters.AddWithValue("@txtContact_PH", txtcontact.Text);
                                cmd.Parameters.AddWithValue("@txtItem_PH", txtitem.Text);
                                cmd.Parameters.AddWithValue("@txtDOB", txtDOB.Value.ToString());
                                //Always use parameterized command queries!! And use add with value since add is depreciated
                                cmd.ExecuteNonQuery(); //Execute the query
                            }

OP is missing ToString() on .Value

On the first line of my code you will notice Con, where Con is my connection and this needs to be passed in. And you can define your connection like this ::
public static readonly string cString = "server=localhost;user id=root;password=root;persistsecurityinfo=True;database=visuals_db;port=3306";
        public static SqlConnection Con = new SqlConnection(cString);
For a much more elaborate answer, on how I like to set these up for concurrent commands subject to change parameter value changes as well as the amount of parameters per command, read on...

..................................................................................

If you are like me and prefer to pass in your statements and value parameters separately and have your method count how many parameters you are actually using to construct your SQL command statements for you. You can do this with the following example and this is great for recurring insert/update/delete commands etc which are subject to statement changes and the amount of parameters being inserted. Note: I didn't have access to SQL server, and used a MySQL server instead, but regardless, it will work with both. All you need to do is rename anything with a MySQL on it to SQL. For example, MySqlCommand cmd = new MySqlCommand would become SqlCommand cmd = new SqlCommand and so-on...
Before we start, here is a screenshot of the code working its magic ::

Attached Image

Lets start with a basic winforms application. If you are using SQL server, remove the using directive for MySQL.Data... and respectively so for SQLClient if using the other.
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace TestCSharpApp
{
    public partial class Form1 : Form
    {

    }
}

So we have our partial class which we will work with for simplicity. Generally I like to section out the following code into its own class. But everyone has their own preferred way of structuring their code for database related apps. Put these in the partial class ::
        public static readonly string cString = "server=localhost;user id=root;password=root;persistsecurityinfo=True;database=visuals_db;port=3306";
        public static MySqlConnection Con = new MySqlConnection(cString);
        public static readonly string Insert_Statement = "Insert into CustomerInfo values";
In the forms constructor you are going to paste the following ::
            InitializeComponent();
            string[] arr = new string[4];
            arr[0] = txtCN.Text;
            arr[1] = txtcontact.Text;
            arr[2] = txtitem.Text;
            arr[3] = txtDOB.Value.ToString();
            Insert(Insert_Statement, arr, arr.Length);
What we are doing now is creating an array of strings where [4] is the maximum amount of addresses you will set for our array of four pointers. Each one takes one address. We then set each one with an address of the values on your user interface. (Again, I'd take a different approach with UI interactions, but for the sake of simplicity, we can keep this short, as this way will work fine. But on a side note; I do all my UI interactions on non UI threads.)

You will notice that our insert member function/method takes some arguments. Insert(Insert_Statement, arr, arr.Length);

Insert_Statement is "Insert into CustomerInfo values"; and arr are the strings in which we set earlier and we will pass them into our method along with an integral value which is the array length, and we will utilize this value shortly and you will understand its purpose in the next part. So lets create our Insert_Statement Method ::
public static bool Insert(string Statement, string[] Values, int ParamCount)
        {

        }
And inside this method we will create a try catch block because there may be a number of reasons exceptions will flare up...
try //Do not nest try catch blocks inside each other. You should instead declare more than one catch exception inside one try catch block. For this example we will only put one...
            {
                foreach (string NonNull in Values)
                {
                    if (NonNull == null)
                        ParamCount--;
                }
            }
We iterate the Values for any nulls, and we do this because it is possible for you to only add addresses to only the first 2 pointers in the array. We will then decrease the ParamCount integer per each null value found to accurately reflect how many parameters with values are actually in the array.

The ParamCount will be used as a switch case statement for us to accurately insert the correct number of parameters as well as constructing the proper MySQL statement to pass onto the MySQLCommand, and each case inside the switch is different and so are the statements. Ok, lets take a look ::
                    if (Con != null)
                //Check connection reachable and open it if its closed.
                {
                    DoConnection(); //While the connection is closed, we will open it
                    switch (ParamCount)
                    {
                        case 0:
                             //Nothing to insert
                        case 1:
                            using (MySqlCommand cmd = new MySqlCommand(string.Concat(Statement, "(@txtCN_PH)"), Con))
                            {   //Use using blocks for they are self disposing when done using
                                //The below parameters takes the parameter name, and then the value of that parameter
                                //So for the first parameter, we are setting the Name of the person for Fname parameter name
                                cmd.Parameters.AddWithValue("@txtCN_PH", Values[0]);
                                //Always use parameterized command queries!! And use add with value since add is depreciated
                                cmd.ExecuteNonQuery(); //Execute the query
                            }
                            DoConnection(); //Now run this method, to close the connection.
                            return true;
                        case 2:
                            using (MySqlCommand cmd = new MySqlCommand(string.Concat(Statement, "(@txtCN_PH,@txtContact_PH)"), Con))
                            {   //Use using blocks for they are self disposing when done using
                                //The below parameters takes the parameter name, and then the value of that parameter
                                //So for the first parameter, we are setting the Name of the person for Fname parameter name
                                cmd.Parameters.AddWithValue("@txtCN_PH", Values[0]);
                                cmd.Parameters.AddWithValue("@txtContact_PH", Values[1]);
                                //Always use parameterized command queries!! And use add with value since add is depreciated
                                cmd.ExecuteNonQuery(); //Execute the query
                            }
                            DoConnection(); //Now run this method, to close the connection.
                            return true;
                        case 3:
                            using (MySqlCommand cmd = new MySqlCommand(string.Concat(Statement, "(@txtCN_PH,@txtContact_PH,@txtItem_PH)"), Con))
                            {   //Use using blocks for they are self disposing when done using
                                //The below parameters takes the parameter name, and then the value of that parameter
                                //So for the first parameter, we are setting the Name of the person for Fname parameter name
                                cmd.Parameters.AddWithValue("@txtCN_PH", Values[0]);
                                cmd.Parameters.AddWithValue("@txtContact_PH", Values[1]);
                                cmd.Parameters.AddWithValue("@txtItem_PH", Values[2]);
                                //Always use parameterized command queries!! And use add with value since add is depreciated
                                cmd.ExecuteNonQuery(); //Execute the query
                            }
                            DoConnection(); //Now run this method, to close the connection.
                            return true;
                        case 4:
                            using (MySqlCommand cmd = new MySqlCommand(string.Concat(Statement, "(@txtCN_PH,@txtContact_PH,@txtItem_PH,@txtDOB)"), Con))
                            {   //Use using blocks for they are self disposing when done using
                                //The below parameters takes the parameter name, and then sets the value of that parameter
                                //So for the first parameter, we are setting the parameter "txtCN_PH" to the value of Values[0] and then so on for each.
                                cmd.Parameters.AddWithValue("@txtCN_PH", Values[0]);
                                cmd.Parameters.AddWithValue("@txtContact_PH", Values[1]);
                                cmd.Parameters.AddWithValue("@txtItem_PH", Values[2]);
                                cmd.Parameters.AddWithValue("@txtDOB", Values[3]);
                                //Always use parameterized command queries!! And use add with value since add is depreciated
                                cmd.ExecuteNonQuery(); //Execute the query
                            }
                            DoConnection(); //Now run this method, to close the connection.
                            return true;
                    }
                }
First we check that our current connection is not null, and then we call DoConnection() - This method is a simple switch which works off of a bool value. And so ::
public static bool DoConnection() //Open and close the connection with this method
        {
            //We devised a switch statement to open and close the connection depending on its current state
            switch (Con.State == ConnectionState.Closed)
            {
                case true:
                    Con.Open();
                    return true;

                case false:
                    Con.Close();
                    return false;
            }
            return false;
        }
Depending on its current Con.State, the connection will open or close. And as the code continues to run, depending on the amount of parameters set by our foreach iteration count, the executing case will be the case value to be executed. Only one case can be executed per execution. Now might be a good time to point out how statements are constructed, so lets look at one of them ::
using (MySqlCommand cmd = new MySqlCommand(string.Concat(Statement, "(@txtCN_PH)"), Con))
In this code, you will notice and remember that our Statement value passed in was "Insert into CustomerInfo values" with no values in the statement. I done this so I could construct the second half of the statement based on its ParamCount and non null values. This is why we use String.Concat to combine Statement with the first value(s) of the placeholder to construct our values section of our MySQL/SQL statement.

This is a great little method worth playing with. Because you really can customize it to suit a variety of situations, and its really handy if you have statements which should be subject to change, and or if you are running a continuous loop which does a variety of MySQL/SQL commands which do not always have the same values or amount of values per each execution. I just thought I'd share this, as it may help some people out who are new to working with databases and parameters. If you have any suggestions or contributions or questions, post them up.

Full Code ::
Spoiler

This post has been edited by Sheepings: 13 May 2019 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

#27 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: Does not contain a definition for "Value"

Posted 23 May 2019 - 08:27 PM

Assuming that c0pking is using a consistent Hungarian naming convention for his controls, then the prefix txt would indicate that the control is a TextBox. The TextBox controls DO NOT have a Value property which is exactly what the compiler is complaining about. It doesn't matter if you try to call ToString() on a non-existent property like what RushabhVerma and Sheepings were trying to say on post #24 and #26 respectively -- it still won't work.

Now assuming that c0pking did make a typo and used the wrong Hungarian of txt instead of dtp for a DateTimePicker, then the bigger mystery is why would the compiler complain that that it does not have the definition for Value since the DateTimePicker.Value property does exist.
Was This Post Helpful? 0
  • +
  • -

#28 RushabhVerma   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 13
  • Joined: 23-April 19

Re: Does not contain a definition for "Value"

Posted 06 June 2019 - 10:05 PM

If you are taking date in textbox u will have to create your own datetime object from textbox's entered value.
Was This Post Helpful? 0
  • +
  • -

#29 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: Does not contain a definition for "Value"

Posted 06 June 2019 - 10:45 PM

True, but just blindly adding the .Value after the variable referencing the TextBox won't magically create that DateTime. The compiler can't read your mind to figure out that was your intent.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2