[英]How to insert foreign key data into database using c# asp.net
I'm new to C# and I was trying to insert foreign key data to database using C# in ASP.NET but I'm not able to add the data to database.我是 C# 的新手,我试图使用 ASP.NET 中的 C# 将外键数据插入数据库,但我无法将数据添加到数据库。
Here is my code:这是我的代码:
protected void SaveButton_Click(object sender, EventArgs e)
{
con.Open();
int overRide = OverRideCheckBox.Checked ? 1 : 0;
int r = 0;
decimal r1 = 0;
string query1 = "INSERT INTO Project_Productivity (ProjectInformationId_FK, PastExperienceInDomain, PastExperienceInTechnology, MultipleTechnologiesOrPlatforms, ClarityOfRequirements, AverageAgeOfApplication, QualityOfApplication, ResourceCapability, UsageOfProductivityTools, InterdependenceWithOtherModules, ComplexityInDatabaseChanges, TestEnvironmentStability, ComplexityOfTesting, BusinessCriticality, IsOverrideCalculation, ProductivityFactor, PermanentRatio, RotationalRatio, OffshoreRatio, Remarks) VALUES ((SELECT ProjectInformationId_PK FROM Project_Information WHERE ProjectInformationId_PK = @ProjectInformationId_FK), @PastExperienceInDomain, @PastExperienceInTechnology, @MultipleTechnologiesOrPlatforms, @ClarityOfRequirements, @AverageAgeOfApplication, @QualityOfApplication, @ResourceCapability, @UsageOfProductivityTools, @InterdependenceWithOtherModules, @ComplexityInDatabaseChanges, @TestEnvironmentStability, @ComplexityOfTesting, @BusinessCriticality, @IsOverrideCalculation, @ProductivityFactor, @PermanentRatio, @RotationalRatio, @OffshoreRatio, @Remarks)";
using (SqlCommand cmd = new SqlCommand(query1))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@ProjectInformationId_FK", int.TryParse(ProjectInformationIdtxt.Text, out r));
cmd.Parameters.AddWithValue("@PastExperienceInDomain", int.TryParse(PastExperienceDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@PastExperienceInTechnology", int.TryParse(PastTechnologyDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@MultipleTechnologiesOrPlatforms", int.TryParse(MultipleDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@ClarityOfRequirements", int.TryParse(CompletenessDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@AverageAgeOfApplication", int.TryParse(AverageAgeDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@QualityOfApplication", int.TryParse(QualityDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@ResourceCapability", int.TryParse(ResourceDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@UsageOfProductivityTools", int.TryParse(UsageDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@InterdependenceWithOtherModules", int.TryParse(InterdependenceDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@ComplexityInDatabaseChanges", int.TryParse(ComplexityDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@TestEnvironmentStability", int.TryParse(TestEnvironmentDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@ComplexityOfTesting", int.TryParse(ComplexityTestingDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@BusinessCriticality", int.TryParse(CriticalityDropDown.SelectedItem.Value, out r));
cmd.Parameters.AddWithValue("@IsOverrideCalculation", overRide);
cmd.Parameters.AddWithValue("@ProductivityFactor", decimal.TryParse(ProdFactorTextBox.Text, out r1));
cmd.Parameters.AddWithValue("@PermanentRatio", int.TryParse(PermanentTextBox.Text, out r));
cmd.Parameters.AddWithValue("@RotationalRatio", int.TryParse(RotationalTextBox.Text, out r));
cmd.Parameters.AddWithValue("@OffshoreRatio", int.TryParse(OffshoreTextBox.Text, out r));
cmd.Parameters.AddWithValue("@Remarks", RemarksTextBox.Text);
cmd.ExecuteNonQuery();
lblmsg.Text = "Project Productivity - Saved Successfully";
}
}
But I'm getting this error:但我收到了这个错误:
Can anyone help with this?有人能帮忙吗?
Thank you in advance.先感谢您。
what us the purpose of the inline subquery我们内联子查询的目的是什么
((SELECT ProjectInformationId_PK FROM Project_Information WHERE ProjectInformationId_PK = @ProjectInformationId_FK)
I believe it is redundant,you are assigning the same value to ProjectInformationId_FK again, so you can omit it and user the following我认为这是多余的,您再次为 ProjectInformationId_FK 分配相同的值,因此您可以省略它并使用以下内容
values ( @ProjectInformationId_FK, @PastExperienceInDomain, @PastExperienceInTechnology, @MultipleTechnologiesOrPlatforms, @ClarityOfRequirements, @AverageAgeOfApplication, @QualityOfApplication, @ResourceCapability, @UsageOfProductivityTools, @InterdependenceWithOtherModules, @ComplexityInDatabaseChanges, @TestEnvironmentStability, @ComplexityOfTesting, @BusinessCriticality, @IsOverrideCalculation, @ProductivityFactor, @PermanentRatio, @RotationalRatio, @OffshoreRatio, @Remarks)";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.