Asp.net 在数据库中插入包含3个外键的记录
我试图在表中插入一条记录,但问题是表中有3个外键。我将为此使用dataTable,但它会给出一个错误“0处没有行位置” 这是我的代码:Asp.net 在数据库中插入包含3个外键的记录,asp.net,Asp.net,我试图在表中插入一条记录,但问题是表中有3个外键。我将为此使用dataTable,但它会给出一个错误“0处没有行位置” 这是我的代码: try { using (var sqlConnection = new Helpers.SqlConnectionHelpers()) { var connection = sqlConnection.OpenConnection();
try
{
using (var sqlConnection = new Helpers.SqlConnectionHelpers())
{
var connection = sqlConnection.OpenConnection();
command = new SqlCommand("CarSold_Insert", connection);
command.CommandType = CommandType.StoredProcedure;
SqlCommand commandTwo = new SqlCommand("SELECT CarForSaleId, UserId, SalesPersonId FROM CarSold WHERE Price = '" + txtPrice.Text + "'", connection);
DataTable table = new DataTable();
DataSet dataSet = new DataSet();
table.Load(commandTwo.ExecuteReader());
var carForSaleId = table.Rows[0]["CarForSaleId"].ToString();
var userId = table.Rows[0]["UserId"].ToString();
var salesPersonId = table.Rows[0]["SalesPersonId"].ToString();
command.Parameters.AddWithValue("CarForSaleId", carForSaleId);
command.Parameters.AddWithValue("UserId", userId);
command.Parameters.AddWithValue("SalesPersonId", salesPersonId);
command.Parameters.AddWithValue("Price", txtPrice.Text);
command.Parameters.AddWithValue("DateSold", dateSold);
command.Parameters.AddWithValue("MonthlyPaymentDate", monthlyPaymentDate);
command.Parameters.AddWithValue("MonthlyPaymentAmount", txtPaymentAmount.Text);
//connection.Open();
int k = command.ExecuteNonQuery();
command.Parameters.Clear();
if (k != 0)
{
Response.Redirect("~/AdminPanel/CarSold.aspx");
}
else
{
lblAns.Text = "Record Not Inserted into the database";
lblAns.ForeColor = System.Drawing.Color.Red;
}
}
}
catch (Exception ex)
{
lblAns.Text = ex.Message;
}
由于Price列是float,您需要使用如下查询参数编写查询:
SqlCommand commandTwo = new SqlCommand("SELECT CarForSaleId, UserId, SalesPersonId FROM CarSold WHERE Price = @Price", connection);
commandTwo.Parameters.AddWithValue("@Price", txtPrice.Text);
上述命令将返回您将在insert命令中使用的
carForSaleId
、userId
、salersonid
,因为价格列是浮动的,所以您需要使用如下查询参数编写查询:
SqlCommand commandTwo = new SqlCommand("SELECT CarForSaleId, UserId, SalesPersonId FROM CarSold WHERE Price = @Price", connection);
commandTwo.Parameters.AddWithValue("@Price", txtPrice.Text);
上述命令将返回您将在insert命令中使用的
carForSaleId
、userId
、salersonid
,我从您的代码中了解到,您正在尝试获取插入数据库的相同记录,因此您应该运行command.ExecuteNonQuery();在select语句之前。不,我没有尝试插入相同的记录,它只是我必须从中获取的外键。@PratikshaPansara是Price列数据类型varchar?我注意到您的查询是从carsell中选择CarForSaleId、UserId、salersonid,其中Price='{txtPrice.Text}'
。确保上面的select语句返回一个或多个rowsDatatype of Price列是浮动的,然后您需要修复select查询SqlCommand commandTwo=new SqlCommand(“从CARSELD中选择CarForSaleId、UserId、SALESERSONID,其中Price=“+txtPrice.Text,connection”)代码>我从您的代码中了解到,您正在尝试获取插入到数据库中的相同记录,为此,您应该运行command.ExecuteNonQuery();在select语句之前。不,我没有尝试插入相同的记录,它只是我必须从中获取的外键。@PratikshaPansara是Price列数据类型varchar?我注意到您的查询是从carsell中选择CarForSaleId、UserId、salersonid,其中Price='{txtPrice.Text}'
。确保上面的select语句返回一个或多个rowsDatatype of Price列是浮动的,然后您需要修复select查询SqlCommand commandTwo=new SqlCommand(“从CARSELD中选择CarForSaleId、UserId、SALESERSONID,其中Price=“+txtPrice.Text,connection”)
它不起作用,因为我获取了Id,但我想插入记录。问题是您的commandTwo
sqlCommand没有返回任何基于您用作筛选条件的价格值的数据。请确保在txtPrice
textbox中输入的价格值中存在数据。它不起作用,因为我获取了Id,但我想插入记录。问题是您的commandTwo
sqlCommand没有返回任何基于您用作筛选条件的价格值的数据。确保有与您在txtPrice
文本框中键入的价格值相对应的数据