我有两个表,一个是教师,另一个是调度。教师表具有主键TID和其他列。 Scheduling表具有主键,其他列和外键TID。
现在,我将教师数据插入一个页面。它成功插入。我必须在其他页面中插入数据,以便与学生一起安排教师的课程。我的问题是这个,我不能在调度表的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);
}
}
}
}
那么有人可以帮助我吗?