I have a window in WPF. When I user enter name, I want to insert it to database and get USERID.
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();
OleDbCom开发者_开发技巧mand 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.**
}
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.
精彩评论