1. #1

    C# and SQL insert with DateTimePicker and ComboBox

    Hello MMO-Champ-ers,

    It's been a while since I've posted on these forums and I'm probably going to do a long shot as I'm unsure how many users might understand C# and SQL (as well as ASP.Net and all that jazz) but I'm going to a shot in the dark.

    I'm having a little trouble applying the index to the following if anybody could shed some light on the matter.

    Code:
            public DataRow CarAdd { get; set; }
    
            private void Car_Load(object sender, EventArgs e)
            {
                if (CarAdd != null)
                {
                    CarID.Text = CarAdd["CarModelID"].ToString();
                    CarReg.Text = CarAdd["RegNo"].ToString();
                    CarYear.Text = CarYear["RegYearDate"].ToString();
                    CarDescription.Text = CarAdd["Description"].ToString();
                    CarMileage.Text = CarAdd["Mileage"].ToString();
                    CarColour.Text = CarAdd["Colour"].ToString();
                    CarType.Text = CarType["TranmissionType"].ToString();
                    CarFuel.Text = CarFuel["FuelType"].ToString();
                    CarEngine.Text = CarEngine["EngineSize"].ToString();
                    CarDoors.Text = CarDoors["NumberDoors"].ToString();
                    CarPrice.Text = CarAdd["ListPrice"].ToString();
                    CarStatus.Text = CarStatus["Status"].ToString();
                }
            }
    This is the current code, the error messages being shown are:

    Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.DateTimePicker'
    Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.ComboBox'
    Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.ComboBox'
    Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.ComboBox'
    Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.ComboBox'
    Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.ComboBox'

    Within the code above, the fields in which use the DateTimePicker is the RegYearDate, and the fields which use the ComboBox are TransmissionType, FuelType, EngineSize, NumberDoors, Status.

    I can't for the life of me figure out why this code won't work, any help or suggestions would be very much appreciated.

    Thank you in advance

  2. #2
    textbox is: textbox.text = ""

    Add items to combo box drop down: Combobox.items.add("DATA") - to add them to a combo box

    Set current value to combo box:combobox.text = fills combo box with a string.

    (Dim reg As Date = "DD / Month / YYYY") (Maybe; public date reg = "10 February 2013" for C#)

    Set date on DTP: DateTimePicker.value = reg
    Last edited by Yohassakura; 2013-02-11 at 12:09 AM.
    Computer: Intel I7-3770k @ 4.5GHz | 16GB 1600MHz DDR3 RAM | AMD 7970 GHz @ 1200/1600 | ASUS Z77-V PRO Mobo|

  3. #3
    Quote Originally Posted by Yohassakura View Post
    textbox is: textbox.text = ""

    Add items to combo box drop down: Combobox.items.add("DATA") - to add them to a combo box

    Set current value to combo box:combobox.text = fills combo box with a string.

    (Dim reg As Date = "DD / Month / YYYY") (Maybe; public date reg = "10 February 2013" for C#)

    Set date on DTP: DateTimePicker.value = reg
    So how would I change that with the code above or would that need to be done in my DataManager? Sorry we're studying this in University so only been using it properly this year. If could the changes in my code and put the red so I can see where I went wrong it would be amazing

  4. #4
    Quote Originally Posted by Needsy View Post
    So how would I change that with the code above or would that need to be done in my DataManager? Sorry we're studying this in University so only been using it properly this year. If could the changes in my code and put the red so I can see where I went wrong it would be amazing
    Well, I haven't done C# but I'm assuming from what you've written you're trying to pull those car parts from an SQL database, and you want to show them on a form?

    If you just want to display items from a database onto a form:

    Code:
    if (CarAdd != null)
        {
            private date CarYear = CarYear["RegYearDate"]
    
            CarID.Text = CarAdd["CarModelID"].ToString();
            CarReg.Text = CarAdd["RegNo"].ToString();
            DTPCarYear.value = CarYear;  ( DTP = Date Time Picker Object)
            CarDescription.Text = CarAdd["Description"].ToString();
            CarMileage.Text = CarAdd["Mileage"].ToString();
            CarColour.Text = CarAdd["Colour"].ToString();
            CarType.Text = CarType["TranmissionType"].ToString();
            CMBCarFuel.Items.Add = CarFuel["FuelType"].ToString();
            CMBCarEngine.Items.Add = CarEngine["EngineSize"].ToString();
            CMBCarDoors.Items.Add = CarDoors["NumberDoors"].ToString();
            CarPrice.Text = CarAdd["ListPrice"].ToString();
            CMBCarStatus.Items.Add = CarStatus["Status"].ToString();
        }
    Then select the combobox items from the combo box drop downs. There should only be one type in there, so then if you select that type, it'll appear up front.

    Though, i'm not sure why you want combo boxes for those, as you'll only get the specific one to that car, which is in your database.
    Computer: Intel I7-3770k @ 4.5GHz | 16GB 1600MHz DDR3 RAM | AMD 7970 GHz @ 1200/1600 | ASUS Z77-V PRO Mobo|

  5. #5
    No this is to add the car to the sql database.

  6. #6
    Quote Originally Posted by Needsy View Post
    No this is to add the car to the sql database.
    Then, you were going about it completely the wrong way.

    Code:
    ADD CAR TO DATABASE:
    
    - Setting connection to database -
    
    SqlConnection myConnection = new SqlConnection("user id=username;" + 
                                           "password=password;server=serverurl;" + 
                                           "Trusted_Connection=yes;" + 
                                           "database=database; " + 
                                           "connection timeout=30");
    
    - Open connection to database -
    
    try
    {
        myConnection.Open();
    }
    catch(Exception e)
    {
        (e.ToString());
    }
    
    - Insert data into database -
    
    SqlCommand myCommand= new SqlCommand("INSERT INTO table (Column1, Column2) " +
                                         "Values ('string', 1)", myConnection);
    
    OR IN YOUR CASE:
    
    SqlCommand myCommand= new SqlCommand("INSERT INTO table (CarID, CarReg, CarYear, CarDescription, etc) " + "Values (CarID.text, carReg.text, DTPCarYear.value, CarDescription.text, etc)", myConnection);

    I think you can leave the column names out, if you're adding all of the database column values in, that's more to specify which column you want to insert the relative value into.
    Computer: Intel I7-3770k @ 4.5GHz | 16GB 1600MHz DDR3 RAM | AMD 7970 GHz @ 1200/1600 | ASUS Z77-V PRO Mobo|

  7. #7
    I've already done all this as shown below from my AddCarDataManager below shows a portion of the code not all of it, but you get the jist

    Code:
    namespace CarsULike
    {
        class AddCarDataManager
        {
            static SqlCommand GetSelectCommand()
            {
                SqlConnection connection = CarsULikeDB.GetConnection();
                String sqlText = "SELECT * FROM Car";
                SqlCommand command = new SqlCommand(sqlText, connection);
                return command;
            }
            static SqlCommand GetInsertCommand()
            {
                SqlConnection connection = CarsULikeDB.GetConnection();
                String sqlText = "INSERT INTO Car (CarModelID, RegNo, RegYearDate Description, Mileage, Colour, TransmissionType, FuelType, NumberDoors, ListPrice, Status) VALUES (@CarID, @CarReg, @CarYear, @CarDescription, @CarMileage, @CarColour, @CarType, @CarFuel, @CarEngine, @CarDoors, @CarPrice, @CarStatus)";
    
                SqlCommand command = new SqlCommand(sqlText, connection);
                command.Parameters.Add("@CarID", System.Data.SqlDbType.VarChar, 3, "CarModelID");
                command.Parameters.Add("@CarReg", System.Data.SqlDbType.VarChar, 8, "RegNo");
                command.Parameters.Add("@CarYear", System.Data.SqlDbType.VarChar, 30, "RegYearDate");
                command.Parameters.Add("@CarDescription", System.Data.SqlDbType.VarChar, 100, "Description");
                command.Parameters.Add("@CarMileage", System.Data.SqlDbType.VarChar, 6, "Mileage");
                command.Parameters.Add("@CarColour", System.Data.SqlDbType.VarChar, 10, "Colour");
                command.Parameters.Add("@CarType", System.Data.SqlDbType.VarChar, 10, "TransmissionType");
                command.Parameters.Add("@CarFuel", System.Data.SqlDbType.VarChar, 8, "FuelType");
                command.Parameters.Add("@CarEngine", System.Data.SqlDbType.VarChar, 4, "EngineSize");
                command.Parameters.Add("@CarDoors", System.Data.SqlDbType.VarChar, 1, "NumberDoors");
                command.Parameters.Add("@CarPrice", System.Data.SqlDbType.VarChar, 8, "ListPrice");
                command.Parameters.Add("@CarStatus", System.Data.SqlDbType.VarChar, 10, "Status");
    
                return command;
            }
    
            public static DataTable Cars()
            {
                SqlCommand command = GetSelectCommand();
                DataTable carstable = null;
    
                using (command.Connection)
                {
                    command.Connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        carstable = new DataTable();
                        carstable.Load(reader);
                    }
                }
                return carstable;
            }
            public static Boolean AddNewCar(double CarID, String CarReg, String CarYear, String CarDescription, double CarMileage, String CarColour, String CarType, String CarFuel, String CarEngine, int CarDoors, double CarPrice, String CarStatus)
            {
                try
                {
                    SqlCommand command = GetInsertCommand();
                    command.Parameters["@CarID"].Value = CarID;
                    command.Parameters["@CarReg"].Value = CarReg;
                    command.Parameters["@CarYear"].Value = CarYear;
                    command.Parameters["@CarDescription"].Value = CarDescription;
                    command.Parameters["@CarMileage"].Value = CarMileage;
                    command.Parameters["@CarColour"].Value = CarColour;
                    command.Parameters["@CarType"].Value = CarType;
                    command.Parameters["@CarFuel"].Value = CarFuel;
                    command.Parameters["@CarEngine"].Value = CarEngine;
                    command.Parameters["@CarDoors"].Value = CarDoors;
                    command.Parameters["@CarPrice"].Value = CarPrice;
                    command.Parameters["@CarStatus"].Value = CarStatus;

  8. #8
    Quote Originally Posted by Needsy View Post
    I've already done all this as shown below from my AddCarDataManager below shows a portion of the code not all of it, but you get the jist
    So, what is this "indexing" that you're trying to do?
    Computer: Intel I7-3770k @ 4.5GHz | 16GB 1600MHz DDR3 RAM | AMD 7970 GHz @ 1200/1600 | ASUS Z77-V PRO Mobo|

  9. #9
    Quote Originally Posted by Yohassakura View Post
    So, what is this "indexing" that you're trying to do?
    Well I'm getting the error:

    Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.DataTimePicker'
    and
    Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.ComboBox'

    The ComboBox error comes for all the ones that have a combo box being used.

  10. #10
    Quote Originally Posted by Needsy View Post

    Code:
     
           CarYear.Text = CarYear["RegYearDate"].ToString();
    You have the same object (CarYear) used on both left and right side of the expression
    Shouldn't it be

    Code:
     CarYear.Text = CarAdd["RegYearDate"].ToString();
    You are trying to index a textbox or some other type of control which doesn't suport it

  11. #11
    Quote Originally Posted by burek View Post
    Code:
     CarYear.Text = CarAdd["RegYearDate"].ToString();
    Oh dear lord, didn't see I made that mistake on those occasions. It got rid of the errors. Now need to work on the code to find out why the insert isn't working properly. Thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •