在另一个问题中,我问过如何配置ExecuteScalar
并使用SCOPE_INDENTITY
来抓取我新创建的PK PersonID
。将一个人输入到表中的第一组插入语句正在工作,而PersonID
在新创建的记录中位于#39。当我在AddNewCustomer方法上设置断点并完成代码时,newPersonID
设置为“1”并始终保持在那里。有没有人知道为什么它没有为我newPersonID
新创建的记录显示“39或40”。
protected void AddNewCustomer(object sender, EventArgs e)
{
string nFirstName = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
string nLastName = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
string nEmergency = ((TextBox)GridView1.FooterRow.FindControl("txtEmergency")).Text;
string nCell = ((TextBox)GridView1.FooterRow.FindControl("txtCell")).Text;
string nAge = ((TextBox)GridView1.FooterRow.FindControl("txtAge")).Text;
string nActivityCard = ((TextBox)GridView1.FooterRow.FindControl("txtActivityCard")).Text;
string nInitials = ((TextBox)GridView1.FooterRow.FindControl("txtInitials")).Text;
string nBoat = ((TextBox)GridView1.FooterRow.FindControl("txtBoat")).Text;
string nGroup = ((TextBox)GridView1.FooterRow.FindControl("txtGroup")).Text;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("INSERT INTO Person(FirstName, LastName, Emergency#, Cell#, Age, ActivityCard, CraftType, Initials, Group#) " +
"values(@FirstName, @LastName, @Emergency, @Cell, @Age, @ActivityCard, @Boat, @Initials, @Group); " +
"SELECT SCOPE_IDENTITY();");
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = nFirstName;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = nLastName;
cmd.Parameters.Add("@Emergency", SqlDbType.NChar).Value = nEmergency;
cmd.Parameters.Add("@Cell", SqlDbType.NChar).Value = nCell;
cmd.Parameters.Add("@Age", SqlDbType.NChar).Value = nAge;
cmd.Parameters.Add("@ActivityCard", SqlDbType.NChar).Value = nActivityCard;
cmd.Parameters.Add("@Initials", SqlDbType.NChar).Value = nInitials;
cmd.Parameters.Add("@Boat", SqlDbType.VarChar).Value = nBoat;
cmd.Parameters.Add("@Group", SqlDbType.VarChar).Value = nGroup;
cmd.Parameters.AddWithValue("@Date", TextBox1.Text);
cmd.Parameters.AddWithValue("@Time", ddlTripTime.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@Type", ddlTripType.SelectedItem.ToString());
cmd.Connection = con;
con.Open();
int newPersonID = System.Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText ="insert into TripSchedule(TripType, PersonID, Time, Date) values (@Type, @newPerson, @Time, @Date);" +
"SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
"Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
"FROM Person INNER JOIN " +
"TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = @Date and " +
"TripSchedule.Time = @Time and TripSchedule.TripType = @Type;";
cmd1.Parameters.AddWithValue("@Date", TextBox1.Text);
cmd1.Parameters.AddWithValue("@Time", ddlTripTime.SelectedItem);
cmd1.Parameters.AddWithValue("@Type", ddlTripType.SelectedItem);
cmd1.Parameters.AddWithValue("@newPerson", newPersonID);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
编辑每个OP对另一个答案的评论,这是实际的错误消息:
DataBinding: 'System.Data.DataRowView' does not contain a property with the
name 'FirstName'.
这是我的gridview的第一个ItemTemplate。当我在gridview的文本框中输入新数据时,我收到此错误消息,然后点击我的提交按钮,该按钮链接到我的AddNewCustomer
方法
答案 0 :(得分:2)
您的查询包含2个选择
SqlCommand cmd = new SqlCommand("SELECT Person.PersonID,
...
"SELECT SCOPE_IDENTITY();");
所以当你执行
时int newPersonID = System.Convert.ToInt32(cmd.ExecuteScalar());
你总是得到Person.PersonID为1。
更新:
1)
你得到了 DataBinding:'System.Data.DataRowView'不包含带有的属性 名字'FirstName'。因为您在绑定数据GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
因此,如果您需要绑定任何数据,那么您删除的SELECT应位于查询的末尾。例如:
INSERT ...;
INSERT ...;
SELECT ...
这将更符合逻辑,因为您插入数据然后选择一些结果显示在页面上。
2)你可以将2个INSERT查询“组合”成1个调用。你只需要做
INSERT ...;
INSERT ... VALUES(..., SCOPE_IDENTITY());
SELECT ...
即:
SqlCommand cmd = new SqlCommand("INSERT INTO Person(FirstName, LastName, Emergency#, Cell#, Age, ActivityCard, CraftType, Initials, Group#) " +
"values(@FirstName, @LastName, @Emergency, @Cell, @Age, @ActivityCard, @Boat, @Initials, @Group);"
+ "insert into TripSchedule(TripType, PersonID, Time, Date) values (@Type, SCOPE_IDENTITY(), @Time, @Date);" +
"SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
"Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
"FROM Person INNER JOIN " +
"TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = @Date and " +
"TripSchedule.Time = @Time and TripSchedule.TripType = @Type;";
底线:
将所有sql代码移动到存储过程中。