You are missing an matching column in
StudentCourses table to join it with
RegisterStudent table. e.g.if SN stands for student number you need to have this column available in
StudentCourses table as well then you can use below query to select and insert into AttendanceList table.
CREATE TABLE [dbo].[RegisterStudent](
[SN] [char](10) NOT NULL, --Primary key assuming its student number
[sNr] [int] NOT NULL,
[fName] [nvarchar](max) NOT NULL,
[lName] [nvarchar](max) NOT NULL,
PRIMARY KEY CLUSTERED
(
[SN] ASC
CREATE TABLE [dbo].[StudentCourses](
[SN] [char](10) NOT NULL, -- Foreign key from RegisterStudent
[StudentId] [char](10) NOT NULL,
[CourseId] [char](10) NOT NULL
) ON [PRIMARY]
*UPDATE CODE BELOW-
private void LoadData()
{
string str = "insert into AttendanceList(SN,sNr,fName,lName) select SN,sNr,fName,lName from RegisterStudent p inner join StudentCourses c on c.SN = p.SN where c.CourseId=@CourseId";
cmd.Parameters.AddWithValue("@CourseId", cmbClassId.SelectedValue);
cmd.ExecuteNonQuery();
using(SqlCommand cmd1 = new SqlCommand("select * from AttendanceList","<YOUR_CONNECTION_STRING>"))
{
cmd1.CommandType = SqlCommandType.Text;
cmd1.CommandTimeout = 190;
da = new SqlDataAdapter(cmd1);
ds = new DataSet();
da.Fill(ds);
dt = ds.Tables[0];
....
}
...
}