insert from 2 textboxes

bluechris87

New member
Joined
Jul 17, 2016
Messages
3
Programming Experience
Beginner
i try to insert from 2 textboxes to my Database but something gets wrong
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        String connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.mdb";
        OleDbConnection conn;
        public Form1()
        {
            InitializeComponent();
            conn = new OleDbConnection(connectionstring);
        }


        private void label1_Click(object sender, EventArgs e)
        {


        }


        private void button1_Click(object sender, EventArgs e)
        {


            try
            {


                conn.Open();
                OleDbCommand cmd1 = conn.CreateCommand();
                cmd1.CommandText = "Insert into Table1(user,u_pass) values(?,?)";


                OleDbParameter p1 = new OleDbParameter();
                OleDbParameter p2 = new OleDbParameter();


                cmd1.Parameters.Add(p1);
                cmd1.Parameters.Add(p2);


                p1.Value = textBox1.Text.ToString();
                p2.Value = textBox2.Text.ToString();




                OleDbDataReader reader = cmd1.ExecuteReader();
                MessageBox.Show("Hello " + textBox1.Text.ToString());
              


                


            
                this.Hide();


                reader.Close();


                conn.Close();
            }
            catch
            {
                MessageBox.Show("no result");
            }


        }
    }
}
 
Firstly, a data reader is for reading data. That's only relevant if there's a SELECT statement in your SQL code. If there's no query in your SQL then you call ExecuteNonQuery.

As for the issue, you should never tell us that something goes wrong without telling us what that is and where. If an error occurs then there's an error message and you need to tell us what that is and what line generates it. If there's no error but the behaviour isn't what you expect then you need to tell us exactly what you expect and exactly how the actual behaviour differs from that.

Also, please only post relevant code because you're just making it harder if we have to wade through the rest of your code too. For instance, what possible use could posting an empty Click event handler for a Label have?

There are also a number of ways that you could make your code more succinct. Maybe you like it verbose if that is clearer to you but most people find it easier to read fewer lines, as long as each of those lines is clear. For instance, this:
OleDbCommand cmd1 = conn.CreateCommand();
cmd1.CommandText = "Insert into Table1(user,u_pass) values(?,?)";

could be replaced with this:
var cmd1 = new OleDbCommand("Insert into Table1(user,u_pass) values(?,?)", conn);

and this:
OleDbParameter p1 = new OleDbParameter();
OleDbParameter p2 = new OleDbParameter();


cmd1.Parameters.Add(p1);
cmd1.Parameters.Add(p2);


p1.Value = textBox1.Text.ToString();
p2.Value = textBox2.Text.ToString();

could be replaced with this:
cmd1.Parameters.AddWithValue("p1", textBox1.Text);
cmd1.Parameters.AddWithValue("p2", textBox2.Text);

or, if you want to be specific about the data type, something like this:
cmd1.Parameters.Add("p1", OleDbType.VarChar, 50).Value = textBox1.Text;
cmd1.Parameters.Add("p2", OleDbType.VarChar, 50).Value = textBox2.Text;

One thing you should definitely stop doing is calling ToString on Strings. It doesn't actually hurt but it's pointless and it suggests that you don't actually know what data types you're working with. The point of calling ToString is to create a String representation of an object, generally for display purposes. The Text property of a TextBox is type String though, so what do you think you're accomplishing by calling ToString on a String?
 
Back
Top Bottom