0

I have a main form which has an Add Teacher button and an Add subject button which directs them to their respective forms.The add teacher button works perfectly fine but when i click the add subject button it shows error: Cannot find table at position 1.I have been following the same procedure in add subject button as i did in add teacher button .Also I first added tbl_teachers table and then tbl_subjects table in my database ,so technically tbl_teachers should have index 0 right?Also when i click the Data Source section I only see it has only tbl_teachers.How do I update the data source? Thanks in advance.

try
{
    SubjectConnect = new DatabaseConnection();
    conString = Properties.Settings.Default.teachersConnectionString;

    SubjectConnect.connection_string = conString;
    SubjectConnect.sql = Properties.Settings.Default.SQL2;

    ds = SubjectConnect.GetConnection;
    Maxrows = ds.Tables[1].Rows.Count;
}
catch (Exception err)
{
    MessageBox.Show(err.Message,"error");
}

class DatabaseConnection
{

    private string sql_string;
    private string strCon;
    SqlDataAdapter da_1;

    public string sql
    {
        set { sql_string = value; }
    }

    public string connection_string
    {
        set { strCon = value; }
    }

    public DataSet GetConnection
    {
        get { return MyDataset(); }
    }

    private DataSet MyDataset()
    {
        SqlConnection con = new SqlConnection(strCon);
        con.Open();
        da_1 = new SqlDataAdapter(sql_string, con);
        DataSet dat_set = new DataSet();
        da_1.Fill(dat_set,"Table_data_1");
        con.Close();
        return dat_set;
    }

    public void UpdateDatabase(DataSet ds)
    {
        SqlCommandBuilder cb = new SqlCommandBuilder(da_1);
        cb.DataAdapter.Update(ds.Tables[0]);
    }
}
  • Where did you get that **DatabaseConnection** type from? That isn't part of the standard .NET framework. You would need to look into the documentation or source code of that class to get more information (or even the answer) regarding your problem. –  Jan 18 '14 at 04:40
  • I created a DatabaseConnection Class which creates a connection. – Ashutosh Bhardwaj Jan 18 '14 at 04:41
  • Since i can't read your mind, you will have to do it the old-fashioned way of putting the relevant source code of that class into your question (if the class is somewhat bigger, please only put those source code parts in the question which are relevant). Blind-guessing about what that class does is no fun... –  Jan 18 '14 at 04:44
  • I have added the DatabaseConnection Class – Ashutosh Bhardwaj Jan 18 '14 at 05:01
  • Okay. `ds.Tables` is actually a *DataSet*. Look into your DatabaseConnection class, there you only add one single data table to the DataSet (`da_1.Fill(dat_set,"Table_data_1")`). Since there is no second data table added to the *DataSet*, `ds.Tables[1]` will fail. –  Jan 18 '14 at 05:06
  • Also, it is not clear how "Table_data_1" would relate to the things you explained about "tbl_teachers" and "tbl_subjects" (if you have problems with the SQL query not returning the data you want, post the SQL query in the question as well) –  Jan 18 '14 at 05:12
  • So how do I add the second data table in my **DataSet** ? – Ashutosh Bhardwaj Jan 18 '14 at 15:13
  • You might want to look here: [C# DataAdapter and DataSet with multiple table](http://stackoverflow.com/questions/4574606/c-sharp-dataadapter-and-dataset-with-multiple-table) –  Jan 18 '14 at 22:46

0 Answers0