2016-08-12 15 views
0

にデータを挿入するための位置0で何行がありませんデータベースから値を選択して、DS == nullのようにテキストボックスの値iがデータベースに挿入したいが、私は挿入するとき、私がここに をエラー示すならコードですします私は同様の問題に直面していると私は私が多くのことを試してみました、それらを解決する方法を知らないが、私は解決できないこの問題は、まだ実際には問題は私が欲しいです を助けてくださいデータベース

string query = "Select * From Contacts where [Phone No] = " + textBox00.Text + ""; 
        using (OleDbConnection conn = new OleDbConnection(connStr)) 
        { 
         using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn)) 
         { 
          conn.Open(); 
          DataSet ds = new DataSet(); 
          if (ds != null) 
          { 
           adapter.Fill(ds); 
           label128.Text = ds.Tables[0].Rows[0]["Name"].ToString(); 
           setaluefortext00001 = ds.Tables[0].Rows[0]["Phone No"].ToString(); 
           setaluefortext00002 = ds.Tables[0].Rows[0]["Address"].ToString(); 
           conn.Close(); 
          } 
          else 
          { 
           adapter.Fill(ds); 
           connection.Open(); 
           OleDbCommand command1 = new OleDbCommand(); 
           command1.Connection = connection; 
           command1.CommandText = "insert into Contacts ([Name],[Phone No],[Address])values('" + textBox.Text + "'," + textBox00.Text + ",'" + textBox000.Text + "');"; 
           command1.ExecuteNonQuery(); 
           connection.Close(); 
           label128.Text = textBox.Text; 
           setaluefortext00002 = textBox00.Text; 
           setaluefortext00003 = textBox000.Text; 
          } 

答えて

1

あなたのリターンのデータベースは思わ:テーブルが持っています行はありません。あなたが新しいデータセットを割り当てるよう

if (ds != null && ds.tables[0].rows.count>0) 

ので、あなたのコードDSのヌル決して:

も以下のように数える行の検証を入れてくださいDataSet ds = new DataSet();

だから、常に文が実行すると、コードは他に届きません声明

また

if文以上にあなたの adapter.Fill(ds);を置くことができます
string query = "Select * From Contacts where [Phone No] = " + textBox00.Text + ""; 
       using (OleDbConnection conn = new OleDbConnection(connStr)) 
       { 
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn)) 
        { 
         conn.Open(); 
         DataSet ds = new DataSet(); 
         adapter.Fill(ds); 
         if (ds != null && ds.Tables[0].rows.count>0) 
         {         
          label128.Text = ds.Tables[0].Rows[0]["Name"].ToString(); 
          setaluefortext00001 = ds.Tables[0].Rows[0]["Phone No"].ToString(); 
          setaluefortext00002 = ds.Tables[0].Rows[0]["Address"].ToString(); 

         } 
         else 
         {         

          OleDbCommand command1 = new OleDbCommand(); 
          command1.Connection = conn ; 
          command1.CommandText = "insert into Contacts ([Name],[Phone No],[Address])values('" + textBox.Text + "'," + textBox00.Text + ",'" + textBox000.Text + "');"; 
          command1.ExecuteNonQuery();         
          label128.Text = textBox.Text; 
          setaluefortext00002 = textBox00.Text; 
          setaluefortext00003 = textBox000.Text; 
         } 
         conn.Close(); 
+0

テーブルが見つかりません[0] –

+0

大文字の表[0]、 "T"は残念です。申し訳ありませんが試してみましたか? –

+0

yea私は大文字でTを使用していますが、今は正常に動作しています –

関連する問題