none
insert rows in database file using insert query RRS feed

  • Question

  • hi all

     

    I use C# and Acess database file,

    after I add database from data menu --> add new data source

    my database name kal contain one table ( quest ), it have to column ( q_id , questions )

     

    I added query using this procedures :

    opened kaldataset.xsd

    from it I made right click on questTableAdapter , and choose add query

     

    I configered insert query depend on this statment :

    INSERT INTO quest
                          (questions)
    VALUES     (?)

     

    when I click on (exceute query) from (query biuled) window, and insert data in dialogbox

    it work very good

    it's really add data in my access database file;

     

    now I want using this query in my form

    I add commandbutton( butt1) and textbox ( TBox1)

     

    I want that when I click on butt1, the data in TBox1 store in my database file

    exactly like using (exceute query) button

     

    I don't know whats the code I have to write under butt1_click

     

    I opened the kalDataset.Desinger.cs to see the code to (exceute query) button

    I don't find it ! I am also don't know the connectionstring

     

    plz help me

     

    Thursday, August 2, 2007 5:20 AM

All replies

  •  

    in fact

    I configured 2 query

    first one named ( Fill,GrtData(questions) ), it depend on this statement :

    SELECT     q_id, questions

    FROM         qest

    WHERE     (questions LIKE ('%' + ? + '%')

    for this one I insert commandbutton ( butt2) and textbox(TBox2)

    in butt2_click event I wrote this code:

     

    Code Snippet

     

    private void butt2_Click(object sender, EventArgs e)

    {

    try

    {

    this.qestTableAdapter.Fill(this.kalDataSet.qest, TBox2.Text);

    }

    catch (System.Exception ex)

    {

    System.Windows.Forms.MessageBox.Show(ex.Message);

    }

    }

     

     

    this code show the result in listbox

     it work perfect with me

     the second query which I asked above, its name ( InsertQuery(questions) ) it depend on this statement:

    INSERT INTO quest

                          (questions)

    VALUES     (?)

     

    for this one I insert commandbutton ( butt1) and textbox(TBox2)

     

    I wrote this code it butt1_click

     

    Code Snippet

    private void butt2_Click(object sender, EventArgs e)

            { 

                    string constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\khaled.mdb;Integrated Security=SSPI";

                OleDbConnection conn = new OleDbConnection(constring);

                conn.Open();

                string cmd;

                cmd = ("insert into qest (qest) values('" + TBox2.Text + "')");

                OleDbCommand rr = new OleDbCommand(cmd);

                rr.ExecuteNonQuery();

                MessageBox.Show("Done.");

                conn.Close();

                }

     

     

    it give me error message (Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.)

    on ( conn.open(); ) statement

     

    I think  this error because there is pervious connictionstring which the program use

    I don't know it, and I don't sure this is the reason

     

    also

    when I used this code

     

    Code Snippet

    private void but1_Click(object sender, EventArgs e)

            {

                kalDataSet.qestRow newquestRow = kalDataSet.qest.NewqestRow();

                newquestRow.qest = TBox1.Text;

     

                kalDataSet.qest.Rows.Add(newquestRow);

                try

                {

                    this.Validate();

                    this.qestBindingSource.EndEdit();

                    this.qestTableAdapter.Update(this.khaledDataSet.qest);

                    MessageBox.Show("Update successful");

                }

                catch (System.Exception ex)

                {

                    MessageBox.Show("Update failed");

                }

                           

            }

     

     

     

    it's work good in runtime and insert the data in TBox1 in listbox

     but when I check the changes in my access database file

    there in no changes

    the only procedure make changes in database file is click on (execute query) from (query build) window

    as I am shown above

    I don't know where is the wrong

     

    plz help me

    Thursday, August 2, 2007 5:35 PM