Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,I have a question regarding the insertion from one table to another based on the value of the third table.I have 3 tables:
CREATE TABLE [dbo].[AttendanceList](
	[sNr] [int] IDENTITY(1,1) NOT NULL,
	[SN] [char](10) NOT NULL,
	[fName] [nvarchar](max) NOT NULL,
	[lName] [nvarchar](max) NOT NULL,
	[dateArrival] [datetime] NOT NULL,
	[dateDeparture] [datetime] NULL,
	[Attending] [bit] NULL,
	[CourseID] [nvarchar](50) NULL,
	[Departed] [bit] NULL,
PRIMARY KEY CLUSTERED 
(
	[sNr] ASC
CREATE TABLE [dbo].[RegisterStudent](
	[SN] [char](10) NOT NULL,
	[sNr] [int] NOT NULL,
	[fName] [nvarchar](max) NOT NULL,
	[lName] [nvarchar](max) NOT NULL,
	
	
PRIMARY KEY CLUSTERED 
(
	[SN] ASC
CREATE TABLE [dbo].[StudentCourses](
	[StudentId] [char](10) NOT NULL,
	[CourseId] [char](10) NOT NULL
) ON [PRIMARY]

I would like my AttendanceList form to show all the students from RegisterStudent into AttendanceList based on CourseId(which is stored in the third table StudentCourses due to the fact that a student may have multiple courses).How can do this with sql?Thank you in advance.

What I have tried:

This is the method that I have when loading data into Attendance List table:
private void LoadData()
       {

           string str = "insert into AttendanceList(SN,sNr,fName,lName) select SN,sNr,fName,lName from RegisterStudent p inner join StudentCourses c where  c.CourseId='" + cmbClassId.SelectedValue.ToString() + "'";
           da = new SqlDataAdapter(str, connstr);
           ds = new DataSet();
           da.Fill(ds);
           dt = ds.Tables[0];
           bi.DataSource = dt;
           dg.DataSource = bi;
           dg.ClearSelection();
           txtStudentId.Focus();
           var count = Convert.ToInt32(dg.Rows.Count.ToString())-1;
           txtCount.Text = count.ToString();
       }
Posted
Updated 14-Jun-18 3:58am
v2
Comments
Richard Deeming 14-Jun-18 10:05am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

string str = "insert into AttendanceList(SN,sNr,fName,lName) select SN,sNr,fName,lName from RegisterStudent p inner join StudentCourses c where  c.CourseId = @CourseId";
da = new SqlDataAdapter(str, connstr);
da.SelectCommand.Parameters.AddWithValue("@CourseId", cmbClassId.SelectedValue);

1 solution

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]
SQL



*UPDATE CODE BELOW-

C#
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(); // This will insert data in AttendanceList table

  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]; // you will get AttendanceList data to show in data table object
   ....
  }
  ...
}
 
Share this answer
 
v3
Comments
Eliza Maria 14-Jun-18 15:33pm    
Thank you for your response.I tried but i get this error"System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Windows.Forms.ListControl.SelectedValue.get returned null.
"
Eliza Maria 14-Jun-18 15:40pm    
And StudentId is the FK to RegisterStudent
[no name] 15-Jun-18 10:54am    
on first "STR" command it will insert data in your table and later you need to pull data back to show on grid/control. Updated the code now and should have noticed it before :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900