user2774820
user2774820

Reputation: 11

C# Database Insert not working from textbox to listbox

I've made a library management system. Put 4 boxes containing the title, author, no of pages and publisher. And then add a button to add it on the database, and I also have 3 listboxes containing the title, author and publisher. It seems that when I accomplished filling up the 4 textboxes and then clicking the button for insert, it's not updating the listboxes and even the database. Nothing is happening. How do you do that?

     OleDbCommand cmd = new OleDbCommand();
        OleDbConnection cn = new OleDbConnection();
        OleDbDataReader dr;

        public frm1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database2.accdb;Persist Security Info=True";
            cmd.Connection = cn;
            loaddata();
        }

        private void loaddata()
        {
            lstbxTitle.Items.Clear();
            lstbxAuthor.Items.Clear();
            lstbxPub.Items.Clear();

            try
            {
                string q = "SELECT * FROM Table2";
                cmd.CommandText = q;
                cn.Open();
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        lstbxTitle.Items.Add(dr[1].ToString());
                        lstbxAuthor.Items.Add(dr[2].ToString());
                        lstbxPub.Items.Add(dr[6].ToString());

                    }
                }
                dr.Close();
                cn.Close();
            }

            catch (Exception e)
            {
                cn.Close();
                MessageBox.Show(e.Message.ToString());
            }

        }


private void lstbxTitle_Click(object sender, EventArgs e)
        {
            ListBox l = sender as ListBox;
            try
            {
                if (l.SelectedIndex != 1)
                {
                    lstbxTitle.SelectedIndex = l.SelectedIndex;
                    lstbxAuthor.SelectedIndex = l.SelectedIndex;
                    lstbxAuthor.Text = lstbxAuthor.SelectedItem.ToString();
                    lstbxPub.SelectedIndex = l.SelectedIndex;
                    lstbxPub.Text = lstbxPub.SelectedItem.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtbxAddT.Text != "")
            {
                string q = "insert into Table2 (Title) values ('" + txtbxAddT.Text.ToString() + "')";
                txtbxAddT.Text = null;
            }

            if (txtbxAddA.Text != "")
            {
                string q = "insert into Table2 (Author) values ('" + txtbxAddA.Text.ToString() + "')";
                txtbxAddA.Text = null;
            }

            if (txtbxAddNP.Text != "")
            {
                string q = "insert into Table2 (Page Number) values ('" + txtbxAddNP.Text.ToString() + "')";
                txtbxAddNP.Text = null;
            }

            if (txtbxAddP.Text != "")
            {
                string q = "insert into Table2 (Publisher) values ('" + txtbxAddP.Text.ToString() + "')";
                txtbxAddP.Text = null;
            }

            loaddata();
        }

Upvotes: 0

Views: 197

Answers (1)

Shaharyar
Shaharyar

Reputation: 12439

The problem is you are not executing your query. It is in the string q, but you are not executing it anywhere.

Create a method:

private void UpdateData(query)
{
    try 
    { 
        cn.Open(); 
        cmd.CommandText = query; //set query to execute 
        cmd.ExecuteNonQuery();  //executing the query
        cn.Close(); 
    } 
    catch (Exception ex)
    {
    }
}

Call this method in every if-statement of method btnAdd_Click:

if (txtbxAddT.Text != "")
{
    string q = "insert into Table2 (Title) values ('" + txtbxAddT.Text.ToString() + "')";
    UpdateData(q);
    txtbxAddT.Text = String.Empty; //Set it to "Empty", Null is not Empty
}
//... and so on for every if check

Best practice advice:

Assigning textbox.Text to "" or null is not a good practice.

Use String.Empty instead.

While checking for textbox.Text for being Empty, never use textbox.Text == null or textbox.Text == "", these are also not the best practices and some times create problems.

Use String.IsNullOrEmpty(textbox.Text) instead.

Upvotes: 1

Related Questions