views:

87

answers:

0

Hello,

I've to insert a new Row(user) into the Database using SqlDataAdapter. I also have to edit any desired row (whose PID is given). Problem is that both of them are not working.

I've Created an Array of selectQuries[] with quries of the form for INSERT: "SELECT X,Y,Z FROM TAB1 WHERE 0=1". for Editing: "SELECT X,Y,Z FROM TAB1 WHERE PID=213".

        this.selectCommand = new SqlCommand();
        this.selectCommand.CommandType = CommandType.Text;
        this.selectCommand.Connection = this.connection;

        this.dataAdapter = new SqlDataAdapter(this.selectCommand);
        this.dataAdapter.MissingSchemaAction = System.Data.MissingSchemaAction.AddWithKey;

        this.commandBuilder = new SqlCommandBuilder(this.dataAdapter);

            for (int i = 0; i < this.selectQueries.Length; i++)
            {
                this.selectCommand.CommandText = selectQueries[i];
                this.commandBuilder.RefreshSchema();
                this.dataAdapter.Fill(this.dataSet, this.tableNames[i]);
            }

After this I used dataBinding with all the textBoxes to corresponding Columns of each DataTable.

 TextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet, "Patient.Name", true));

In the form after I enter the values into textbox and hit save button. following code executes.

            this.connection.Open();
            for (int j = 0; j < this.selectQueries.Length; j++)
            {
                this.selectCommand.CommandText = this.selectQueries[j];
                this.commandBuilder.RefreshSchema();
                MessageBox.Show(this.commandBuilder.GetInsertCommand(false).CommandText);
                this.dataAdapter.Update(this.dataSet, this.tableNames[j]);
            }
            this.connection.Close();

Now, the problem is that nothing is getting inserted into the DataBase. commandBuilder is generating appropriate INSERT,DELETE,UPDATE queries.

I checked if textboxes are binded correctly by using

For INSERT:

        CurrencyManager cm = (CurrencyManager)this.BindingContext[dataSet, "Patient"];
        long rowPosition = (long)cm.Position;

rowPosition value is -1.

So, How to get away with this? Do I need to insert a blank row programatically into each dataset.Tables[i]? But I dont want to insert a new row with emptystring values. Is there any default way?

For EDITING:

The selected row is being shown in the texboxes correctly. But when I edit and click save. Nothing is getting saved into database. No error too.