Skip to content
Advertisement

Conversion failed when converting the “invalid” value ‘Residential’ to data type int

How do I convert data that is given from result set string. I am trying to show the drop down box gui as a text but give it a value of 1 and 2

what I want it to show as for drop down choice is right: https://i.imgur.com/gDKAkDX.png

but I need the actual values to be 1 and 2 correspondingly

Then I need to put those values back into a database f key that only allows int…

final ObservableList options = FXCollections.observableArrayList();


public void fillComboBox() {
    Connection c;


    try {
        Connection conn = null;
        Statement stmt = null;
        String queryx = "Select CusType_ID, CusTypeName from CustomerType";

        ResultSet jrs = c.createStatement().executeQuery(queryx);

        while (jrs.next()) {

            String custyID = jrs.getString("CusType_ID");
            String custyN = jrs.getString("CusTypeName");


            options.add(new MyObject(custyID,custyN));
            typeBox.setItems(options);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}



String sql = "INSERT INTO Customer [CustomerType_ID]) VALUES" typeBox.getValue() +"')";

Advertisement

Answer

First create a class for storing the CusType_ID and CusTypeName in dropdown(combobox)

public class MyObject {

private String key;
private String value;

public MyObject(String key, String value) {
    this.key = key;
    this.text = value;
}

public String getKey(){
return key ;
}

@Override
public String toString() {
    return value;
  }
}

then store the value from your request

     String queryx = "Select CusType_ID, CusTypeName from CustomerType";

    ResultSet rs = c.createStatement().executeQuery(queryx);

    while (rs.next()) {
        int recordNumber = 1;
        int recNumber =2;

        String sx = rs.getString("CusTypeName");
        String customerID = rs.getString("CusType_ID");


        typeBox.addItem(new MyObject(customerID, sx));


        }

Finally, get your value back from selected item and store it in your new sql

Object item = comboBox.getSelectedItem();
String value = ((MyObject)item).getKey();
String sql = "INSERT INTO Customer (CustomerType_ID) VALUES ("+ 
value +");" ; 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement