.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Join With Insert when we give two table

Posted By: Martin     Posted Date: March 12, 2011    Points:5   Category :C#

actually i made two table 1 is Student(Id(int),Name(varchar),Address(varchar),Mob(int)) and 2 nd table is
Course (Id(int),Types(varchar))..

so now i want one form which have one
TextBox1 for Name ,(user insert date at runtime )
TextBox2 for Mob. (user insert Mob at runtime )
Dropdownlist1 for Types........(ddl select Coursetypes like BE,MCA,CA,BCA, at run time)

Button1 for save(insert)

now i want C# code when user insert then all insert and selected option will be insert in databse..

please sortout my problem...as soon as

Author: SureshM             
Posted Date: March 12, 2011     Points: 20   

Hi Martin,

You have two tables in DB, and One form(UI), the form has 2 text-boxes,1 dropdown and one button for submit.

I think the 2nd table(course) is a master table. U are loading the courses into the dropdown from that table.
One student can have more than one course?, if it is not possible, U can add CourseId column into Table1 and make it as foreign key. Then u can get the course id from the selected value of the dropdown.

strSQL="INSERT INTO Student(Id,Name,Address,Mob,CourseId)VALUES('1','"+texbox1.Text+"','"+texbox2.Text+"','"+dropdownlist.Selectedvalue.ToString()+"')";
SqlConnection objCon = new SqlConnection("ConString");
SqlCommmad objCmd=new SqlCommmad();
objCmd.CommandText = strSQL;
int intRetVal = objCmd.ExecuteNonQuery();

* If u make the Id Columns as Identity, no need to send values.
ex:INSERT INTO (name,add,mob,courid)values(name,add,mob,course)

Author: Martin             
Posted Date: March 12, 2011     Points: 20   

Thanks Suresh i do it then after give the result but here one more question
table is correct?? or not if i would take

Student table
Id int,pk------->This is Primary key

Course table is
Id ---------------->This id make foreign key as you say

am i right?? plz confirm it and What is the "1" It is not changable?? it is remain same

Author: Dhiraj Ranka             
Posted Date: March 14, 2011     Points: 20   

You can rename the Id column from Student table to StudentID to make more understandable.

Also there should be ID for course as well(can be named CourseID) and StudentID(foreign key) in Course table, so that it is easier to understand while you will query them back and forth.

Everything else looks correct to me

Hope this helps

Hope this helps.

Thanks & Regards,
Dhiraj Ranka
http://www.twitter.com/dhirajranka | http://www.dhirajranka.com

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend