i've a code that's inserting but it's not dumping into the database!!!!

    private void buttonX1_Click(object sender, EventArgs e)
    {
        if (textBoxX1.Text != "" && textBoxX2.Text != "" && textBoxX3.Text != "" && textBoxX4.Text!= "" && textBoxX5.Text!= "" && textBoxX6.Text!= "" && textBoxX7.Text!= "" && textBoxX8.Text!= "")
        {
            string s = "Insert Into t1 Values('" + textBoxX1.Text + "','" + textBoxX2.Text + "','" + textBoxX3.Text + "','" + textBoxX4.Text + "','" + textBoxX5.Text + "','" + textBoxX6.Text + "','" + textBoxX7.Text + "','" + textBoxX8.Text + "')";
            con.Open();
            cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;

            cmd = new OleDbCommand(s,con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Inserted");
            textBoxX1.Text = "";
            textBoxX2.Text = "";
            textBoxX3.Text = "";
            textBoxX4.Text = "";
            textBoxX5.Text = "";
            textBoxX6.Text = "";
            textBoxX7.Text = "";
            textBoxX8.Text = "";

            con.Close();

        }
        else
        {

            MessageBox.Show("enter all the fields");
        }
    }

Recommended Answers

All 8 Replies

Inserting data(T-SQL insert) and dumping a database(backup) are unrelated database operations.
Please explain your problem.
BTW: I do not see where your OleDbConnection is instantiated nor where the connection string is specified.

sir it's taking the values correctly, but when i check the database it's not showing the values that i've inserted via buttonX1_click()!!

check the exact database location.

Use try..catch to handle exception.

try with these:

into row = cmd .ExecuteNonQuery();
if(row>0)
MessageBox.Show( "Inserted" );
else
MessageBox. Show( "Failed" );

Obviously it's NOT taking the values correctly.
BTW: What is the schema? You are quoting all values. Only strings (varchar, etc) get enclosed in quotes.
You test for String.Empty but not null.

If select * from t1 does not return the data you think was insterted, then the data was not inserted.

Again, 1)check the connection string. 2) instantiate an OleDbConnection
Per @Riteman:
Identify any Exceptions thrown. Usr try/catch.
ExecuteNonQuery should return the number of rows affected.

Delete lines 7 & 8. This is dead wood.
7: cmd = new OleDbCommand();
8: cmd.CommandType = CommandType.Text;

using System.Data.Ole Db;

namespace F_11

{

public partial class Form1 : Form
{
    OleDbConnection con;
    OleDbDataAdapter da;
    OleDbDataReader dr;
    OleDbCommand cmd;
    public Form1()
    {
        InitializeComponent();
        //Form f1 = new Form();
        con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\MSP\Desktop\F-1\h.mdb");
        this.WindowState = FormWindowState.Maximized;
    }



 private void buttonX1_Click(object sender, EventArgs e)
    {

        if (textBoxX1.Text != "" && textBoxX2.Text != "" && textBoxX3.Text != "" && textBoxX4.Text!= "" && textBoxX5.Text!= "" && textBoxX6.Text!= "" && textBoxX7.Text!= "" && textBoxX8.Text!= "")
        {
            string s = "INSERT INTO t1 VALUES('" + textBoxX1.Text + "','" + textBoxX2.Text + "','" + textBoxX3.Text + "','" + textBoxX4.Text + "','" + textBoxX5.Text + "','" + textBoxX6.Text + "','" + textBoxX7.Text + "','" + textBoxX8.Text + "')";               
                con.Open();
                cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd = new OleDbCommand(s, con);
                int row = cmd.ExecuteNonQuery();                 
if(row>0)
MessageBox.Show( "Inserted" );
else
MessageBox. Show( "Failed" );

                //MessageBox.Show("Inserted");
                textBoxX1.Text = "";
                textBoxX2.Text = "";
                textBoxX3.Text = "";
                textBoxX4.Text = "";
                textBoxX5.Text = "";
                textBoxX6.Text = "";
                textBoxX7.Text = "";
                textBoxX8.Text = "";
                con.Close();            
                      }
        else
        {               
            MessageBox.Show("enter all the fields");
        }
    }

}

sir that's my code !!!!!

My point of view, your problem is in your connection string. A semicolon ";" should be needed after .....\h.mdb" that is like .....\h.mdb;".
Andalso remove the lines

cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;

try this....

using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\MSP\Desktop\F-1\h.mdb"))
                {
if (con.State.Equals(ConnectionState.Closed))
           con.Open();
OleDbCommand Insert = new OleDbCommand("Insert INTO StudentInfo (Tb1,Tb2,Tb3,Tb4,Tb5,Tb6,Tb7,Tb8) Values ('" + textBoxX1.Text + "','" + textBoxX2.Text + "','" + textBoxX3.Text + "','" + textBoxX4.Text + "','" + textBoxX5.Text + "','" + textBoxX6.Text + "','" + textBoxX7.Text + "','" + textBoxX8.Text + "',con);
Insert.ExecuteNonQuery();
MessageBox.Show("Inserted",MessageBoxButtons.OK,MessageBoxIcon.Information);
 con.close();

             }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.