2015-09-14 35 views
0

我有兩個表,一個是Teacher,另一個是Scheduling。教師表具有主鍵TID和其他列。調度表具有主鍵,其他列和外鍵TID。從主鍵asp.net獲取SQL外鍵列值c#

現在,我將教師數據插入到一個頁面中。它插入成功。我必須在其他頁面插入數據以安排學生的老師課程。我的問題是這樣的,我不能在調度表的fk列中插入教師的pk值。

這是我迄今爲止所做的。

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 

namespace E_Tutor_Manager 
{ 
    public partial class ScheduleClass : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      { 
       string connc = @"Data Source=KHAWAR-PC.\SQLEXPRESS;Initial Catalog=ETManager;Integrated Security=True"; 
       SqlConnection con = new SqlConnection(connc); 
       string query = "SELECT Username FROM Teachers"; 
       SqlCommand cmd = new SqlCommand(query, con); 
       cmd.Connection.Open(); 
       SqlDataReader ddlValues; 
       ddlValues = cmd.ExecuteReader(); 
       TUName.DataSource = ddlValues; 
       TUName.DataValueField = "Username"; 
       TUName.DataTextField = "Username"; 
       TUName.DataBind(); 
       cmd.Connection.Close(); 
       cmd.Connection.Dispose(); 
      } 

      if (!IsPostBack) 
      { 
       if (Session["New"] != null) 
       { 
        welcome.Text += Session["New"].ToString(); 
       } 
       else 
        Response.Redirect("Login.aspx"); 
      } 
     } 

     protected void Button1_Schedule_Click(object sender, EventArgs e) 
     { 

      try 
      { 
       { 
        string connc = @"Data Source=KHAWAR-PC.\SQLEXPRESS;Initial Catalog=ETManager;Integrated Security=True"; 
        SqlConnection con = new SqlConnection(connc); 
        con.Open(); 

        string query = "SELECT * FROM Teachers WHERE StartTime = @StartTime"; 
        string query1 = "INSERT INTO Schedules(Grade,Subjects,STime,TID) VALUES ('" + txtGrd.Text + "','" + Sbj.SelectedValue + "','" + txtTime.Value + "')"; 
        SqlCommand sql = new SqlCommand(query1, con); 
        SqlCommand sql1 = new SqlCommand(query, con); 
        sql.ExecuteNonQuery(); 
        var cmd = new SqlCommand(query, con); 
        cmd.Parameters.AddWithValue("@StartTime", txtTime.Value); 
        SqlDataAdapter da = new SqlDataAdapter(cmd); 
        DataTable dt = new DataTable(); 
        da.Fill(dt); 
        if (dt.Rows.Count > 0) 
        { 
         labelmsg.Text = "Sorry!!! This teacher is not available at this time. Please select the other teacher"; 
        } 
        else 
        { 
         labelmsg.Text = "Class Scheduled Succesfully!!!"; 
        } 
        con.Close(); 
       }   
      } 
      catch (Exception ee) 
      { 
       throw (ee); 
      } 
     } 

    } 
} 

那麼,任何人都可以幫助我嗎?

+1

你從哪裏得到'TID'?上面的字符串'query1'似乎沒有包含在它的值中。 –

回答

0
string query1 = "INSERT INTO Schedules(Grade,Subjects,STime,TID) VALUES ('" + txtGrd.Text + "','" + Sbj.SelectedValue + "','" + txtTime.Value + "')"; 

這是您要插入FK的行。 首先,您必須像這樣進行檢索:

int FK_ID; // This is where we store the foreign key 
using(SqlReader reader = query.ExecuteReader()) 
{ 
    while(reader.Read()) 
    { 
     FK_ID = (int)reader["ID"]; // where supposingly ID is the field name 
     //But this FK_ID will be the one you want 
     //only if the query returns a single result, otherwise it will set FK_ID 
     //to the last returned row 
    } 
} 

然後你就可以使用ID來設置它的值QUERY1

1

試試這個。這隻有在選擇查詢只返回一個值時纔有效。

string query1 = "INSERT INTO Schedules(Grade,Subjects,STime,TID) VALUES ('" + txtGrd.Text + "','" + Sbj.SelectedValue + "','" + txtTime.Value + "',(SELECT TID FROM Teachers WHERE StartTime = @StartTime))";