0

I have the following code:

         private void FillData() {
        try {
            // this is the adapter
            dataAdapter = new SqlDataAdapter("Select username, isnull(password,'') from Credentials", connectionString);

            SqlCommandBuilder cmb = new SqlCommandBuilder(dataAdapter);

            // populate new data and bind it.
            DataTable table = new DataTable();
            dataAdapter.Fill(table);
            bindingSource1.DataSource = table;

            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
        }
        catch (Exception e) {
            MessageBox.Show("Error " +e);
        }

    }

    private void ManualGeneratedDGF_Load(object sender, EventArgs e) {
        dataGridView1.DataSource = bindingSource1;
    }

    private void button1_Click(object sender, EventArgs e) {
        bindingSource1 = (BindingSource) dataGridView1.DataSource;
        DataTable ds = new DataTable();
        ds = (DataTable) bindingSource1.DataSource;
        dataAdapter.Update(ds);
    }

I don't understand why the above doesn't populate the code properly when I click the Save Button. I have a username and password int eh datagrid but when I click the Save button, it inserts the username properly but not the password?

SamIAm
  • 2,241
  • 6
  • 32
  • 51
  • I've found the problem to be "isnull(password, ''). HOwever, I don't know how to change it so that when I update, it will update the password column? – SamIAm Jan 27 '15 at 04:58

1 Answers1

0

Just change this line

dataAdapter = new SqlDataAdapter("Select username, isnull(password,'') from Credentials", connectionString);

with

dataAdapter = new SqlDataAdapter("Select username, isnull([password],'') from Credentials", connectionString);

Or where ever you are using password use it with square brackets, because it is reserved word for servers. tell me if it works.

Ammar
  • 152
  • 2
  • 12
  • I tried it and it didnt work. I think it's not because of the reserved word but more because if I use isnull(password,''), the mappings will be thrown off. So how do I remap everything? – SamIAm Jan 27 '15 at 05:43
  • Have you tried `NULLIF` instead of `ISNULL` as I have found it [here](http://stackoverflow.com/questions/334108/how-do-i-check-if-a-sql-server-string-is-null-or-empty) – Ammar Jan 27 '15 at 05:58