tags:

views:

17

answers:

1

hello everyone. i am lilly, i have a problem. i have a window in wpf. when user enter name, i wanna insert it to database and get USERID. but i cant success it. if you help me, i will be so happy. thanks in advance.

private void button1_Click(object sender, RoutedEventArgs e)
        {
            OleDbDataReader rd;
            string name=comboBox1.Text;
            OleDbConnection conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|CellBiology.mdb;Persist Security Info=

True");
            string sql = "select * from UserInformation where UserName='" + name+ "'";
            conn.Open();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            rd = cmd.ExecuteReader();
            if (rd.Read())
            {

                string id = rd["UserID"].ToString();
                MessageBox.Show(id);


            }
            else
            {
                string sql2 = "insert into UserInformation(UserName) values ('" + ad+ "')";
                OleDbCommand ne = new OleDbCommand(sql2, conn);
                ne.ExecuteNonQuery();

**the problem is here.**


     }
A: 

I believe you are never initializing the variable ad you are using in the following SQL:

string sql2 = "insert into UserInformation(UserName) values ('" + ad+ "')";

Which is a problem.

David Oneill