Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > ติดปัญหา insert Foreign key ค่ะรับค่าลงหลายตารางและต้องเก็บ FK ด้วย ใช้ VS2010 C#



 

ติดปัญหา insert Foreign key ค่ะรับค่าลงหลายตารางและต้องเก็บ FK ด้วย ใช้ VS2010 C#

 



Topic : 084189



โพสกระทู้ ( 94 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter



Code (C#)
        protected void btnSave_Click(object sender, EventArgs e)   
        {                                                          
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BD_TrainingConnectionString"].ConnectionString);
            con.Open();
            SqlCommand com = new SqlCommand("insert into Trainners(T_name, T_company, T_phone, T_descrip)values(@T_name, @T_company, @T_phone, @T_descrip)", con);
            com.CommandType = CommandType.Text;
            com.Parameters.AddWithValue("T_name", txtcouTrainner.Text);
            com.Parameters.AddWithValue("T_company", txtcouCompany.Text);
            com.Parameters.AddWithValue("T_phone", txtPhone.Text);
            com.Parameters.AddWithValue("T_descrip", txtDescrip.Text);            
            com.ExecuteNonQuery();
            con.Close();

            con.Open();
            SqlCommand com2 = new SqlCommand("insert into Prices(price_01, price_02, price_03, price_04, price_05, price_06, price_07, price_08, price_09, price_10, price_11, price_12, price_13, price_14, price_15, price_16, result)"
                                            +"values(@price_01, @price_02, @price_03, @price_04, @price_05, @price_06, @price_07, @price_08, @price_09, @price_10, @price_11, @price_12, @price_13, @price_14, @price_15, @price_16, @result)", con);
            com2.CommandType = CommandType.Text;
            com2.Parameters.AddWithValue("price_01", txtP01.Text);
            com2.Parameters.AddWithValue("price_02", txtP02.Text);
            com2.Parameters.AddWithValue("price_03", txtP03.Text);
            com2.Parameters.AddWithValue("price_04", txtP04.Text);
            com2.Parameters.AddWithValue("price_05", txtP05.Text);
            com2.Parameters.AddWithValue("price_06", txtP06.Text);
            com2.Parameters.AddWithValue("price_07", txtP07.Text);
            com2.Parameters.AddWithValue("price_08", txtP08.Text);
            com2.Parameters.AddWithValue("price_09", txtP09.Text);
            com2.Parameters.AddWithValue("price_10", txtP10.Text);
            com2.Parameters.AddWithValue("price_11", txtP11.Text);
            com2.Parameters.AddWithValue("price_12", txtP12.Text);
            com2.Parameters.AddWithValue("price_13", txtP13.Text);
            com2.Parameters.AddWithValue("price_14", txtP14.Text);
            com2.Parameters.AddWithValue("price_15", txtP15.Text);
            com2.Parameters.AddWithValue("price_16", txtP16.Text);
            com2.Parameters.AddWithValue("result", txtCost.Text);
            com2.ExecuteNonQuery();
            con.Close(); 

            
            string fk1 = "Select MAX(TID) From Trinners";
            string fk2 = "Select MAX(priceID) From Prices";
            int tid, pid;
            tid = Convert.ToInt32(fk1);
            pid = Convert.ToInt32(fk2);

            String cou_type = null;

            if (rdInternal.Checked == true)
            {
                cou_type = rdInternal.Text;
            }
            else if (rdExternal.Checked == true)
            {
                cou_type = rdExternal.Text;
            }
                con.Open();
                SqlCommand com3 = new SqlCommand("insert into Courses(cou_name,cou_type,cou_location,cou_date,cou_todate,cou_hour,TID,priceID)"
                                                +"values(@cou_name,@cou_type,@cou_location,@cou_date,@cou_todate,@cou_hour, @tid, @pid)", con);
                com3.CommandType = CommandType.Text;
                com3.Parameters.AddWithValue("cou_name", txtcouName.Text);
                com3.Parameters.AddWithValue("cou_type",cou_type);
                com3.Parameters.AddWithValue("cou_location", txtcouLocation.Text);
                com3.Parameters.AddWithValue("cou_date", DateTime.ParseExact(txtDate.Text.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture));
                com3.Parameters.AddWithValue("cou_todate", DateTime.ParseExact(txtTodate.Text.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture));
                com3.Parameters.AddWithValue("cou_hour", txtcouTime.Text);
                com3.Parameters.AddWithValue("TID", Convert.ToInt32(fk1));
                com3.Parameters.AddWithValue("priceID", Convert.ToInt32(fk2));
                com3.ExecuteNonQuery();
                Label1.Text = "Record Successfully";
                Response.Redirect("Course.aspx");
        }

คือเก็บค่าจากหนึ่งฟอร์มลงหลายตาราง แล้วต้องดึงค่า ID ของสองตารางแรกลงในตารางที่สามด้วย แต่ไม่รู้ว่าจะเขียนยังไง ลองทำมา 2 สัปดาห์แล้วแต่ไม่ได้เลยค่ะ เหลือเวลาทำโปรเจคอีกแค่ 3 week ช่วยด้วยนะคะ



Tag : .NET, C#, VS 2010 (.NET 4.x)









ประวัติการแก้ไข
2012-09-18 13:57:11
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-09-18 13:53:40 By : akura View : 1360 Reply : 2
 

 

No. 1



โพสกระทู้ ( 219 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

คือมี 3 ตาราง ในลักษณะ แบบว่า ตารางตรงกลาง เป็น Bridge Table เข้าใจถูกปะครับ
Bridge Table คือ ดึง FK ของ 2 ตารางข้างๆ มาเป็น PK, FK ของตารางตัวเอง

ถ้าใช่ ก็ต้อง INSERT เข้าไป 2 ตารางก่อน
ส่วนตาราง bridge ทำทีหลังสุด

ลองเขียน Query insert ใน SQL Server ก่อน
ให้มัน execute ที่เดียว 3 insert statement เลย
ถ้ามันทำผ่านก็เอามาแปะใน .cs ถ้าไม่ผ่าน ลองเช็คพวก relationship ใน db ดูว่าถูกต้องมั้ย
พวก constraint integrity ต่างๆ ว่ามันห้าม insert อะไรยังไงรึป่าว

ที่ผมเคยทำก็เคยเจอแบบนี้ล่ะครับ
ตาราง bridge มักจะมีปัญหาเรื่อง key
ตอนนั้นที่ผมแก้คือ
เขียน method อีกอันนึงไว้สำหรับ insert ตัว bridge table
คือตอนที่ execute_click แล้วก็รัน 2 statement
พอรันจบ close conn แล้วไปเรียก อีก method เพื่อ execute อีก statement

ยังไงลอง capture error มาให้ชาว TC ช่วยกันดูด้วยก็ดีนะครับ
เผื่อมีใครมีไอเดียเจ๋งกว่าผม จะได้แชร์ๆ กัน






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-18 15:51:11 By : mixarstudio
 


 

No. 2



โพสกระทู้ ( 94 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter

Code (C#)
 con.Open();
            string fk1,fk2;
            int tid,pid;
            fk1 = "Select MAX(TID) From Trainners";
            SqlCommand cmd = new SqlCommand(fk1,con);
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            tid = Convert.ToInt32(dr[0]);
            dr.Close();


            fk2 = "Select MAX(priceID) From Prices";
            SqlCommand cmd2 = new SqlCommand(fk2, con);
            SqlDataReader dr2 = cmd.ExecuteReader();
            dr2.Read();
            pid = Convert.ToInt32(dr2[0]);
            dr2.Close();

mixarstudio ขอบคุณนะคะ
ลองแก้แบบนี้ดูคิดว่าน่าจะได้แล้ว แต่ตอนนี้มี error ที่ com3.ExecuteNonQuery(); ขึ้นว่า Must declare the scalar variable "@pid".
ไม่รู้ว่าแก้ยังไง รบกวนด้วยนะคะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-18 16:32:29 By : akura
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ติดปัญหา insert Foreign key ค่ะรับค่าลงหลายตารางและต้องเก็บ FK ด้วย ใช้ VS2010 C#
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่