为什么它继续说"找不到表0"?

时间:2014-05-12 05:18:13

标签: c# sql-server visual-studio-2008

我正在创建一个具有add-edit-delete功能的系统,但每当我尝试从我的ms sql server 2005编辑一个值时,它一直告诉我“找不到表0”。下面是我的代码我正在使用visual studio c#2008:

private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            SqlDataAdapter dad = new SqlDataAdapter();
            SqlCommandBuilder scb = new SqlCommandBuilder(dad);

            dad.UpdateCommand = new SqlCommand("UPDATE tblSchools SET Number = @id, School_Name = @school, Province = @prov, City = @city, Brgy = @brgy, Lot_Num = @lot, Area = @area, Mem_Date_Rec = @date, Cenro = @cenro", conn);
            dad.UpdateCommand.Parameters.Add("@school", SqlDbType.VarChar).Value = textBox1.Text;
            dad.UpdateCommand.Parameters.Add("@prov", SqlDbType.VarChar).Value = comboBox1.Text;
            dad.UpdateCommand.Parameters.Add("@city", SqlDbType.VarChar).Value = textBox2.Text;
            dad.UpdateCommand.Parameters.Add("@brgy", SqlDbType.VarChar).Value = textBox4.Text;
            dad.UpdateCommand.Parameters.Add("@lot", SqlDbType.NVarChar).Value = textBox5.Text;
            dad.UpdateCommand.Parameters.Add("@area", SqlDbType.Decimal).Value = textBox6.Text;
            dad.UpdateCommand.Parameters.Add("@date", SqlDbType.DateTime).Value = dateTimePicker1.Value.Date;
            dad.UpdateCommand.Parameters.Add("@cenro", SqlDbType.NVarChar).Value = textBox8.Text;
            dad.UpdateCommand.Parameters.Add("@id", SqlDbType.Int).Value = ds.Tables[0].Rows[tblNamesBS.Position][0];

            conn.Open();
            dad.UpdateCommand.ExecuteNonQuery();
            conn.Close();


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

在代码上方我做了一个全局函数

 DataSet ds = new DataSet();

    SqlConnection conn = new SqlConnection("Data Source=MJ-PC\\SQLEXPRESS;Initial Catalog=Users;Integrated Security=True");


    BindingSource tblNamesBS = new BindingSource();

这里似乎有什么问题?

哦,加起来我做了一个有双击事件的datagridview,下面是我的代码:

private void dg_DoubleClick(object sender, EventArgs e)
    {
        try
        {
            button2.Visible = true;
            button5.Visible = true;
            DataTable dt = new DataTable();
            SqlDataAdapter dad = new SqlDataAdapter("SELECT * FROM tblSchools WHERE Number ="+
                Convert.ToInt16(dg.SelectedRows[0].Cells[0].Value.ToString()) + "", conn);
            dad.Fill(dt);

            textBox1.Text = dt.Rows[0][1].ToString();
            comboBox1.Text = dt.Rows[0][2].ToString();
            textBox2.Text = dt.Rows[0][3].ToString();
            textBox4.Text = dt.Rows[0][4].ToString();
            textBox5.Text = dt.Rows[0][5].ToString();
            textBox6.Text = dt.Rows[0][6].ToString();
            //dateTimePicker1.Value = dt.Rows[0][7];
            textBox8.Text = dt.Rows[0][8].ToString();



        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

此代码会影响我的编辑吗?

1 个答案:

答案 0 :(得分:1)

您已声明空数据集

DataSet ds = new DataSet();

稍后您尝试访问其中的Table[0],但没有定义。