我有一个带有插入功能的gridview,我试图根据有的行数来限制INSERT语句。当前设置方式的问题在于,当我输入新行时,验证检查有效,但该行不包含任何数据,因此将空行插入到gridview中。
当我按照调试器中的代码进行操作时,局部变量都显示为""
,即使我在按下提交之前输入了数据。
当我将代码块移动到AddNewCustomer而没有任何验证时,它可以正常工作。 我尝试过这样的事情:
(MGRchk设置为= datatable.rows.count)
private void BindData()
{
string strQuery = "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 INNER JOIN Bus ON TripSchedule.BusID = Bus.BusID where TripSchedule.Date = '" + TextBox1.Text + "' and " +
"TripSchedule.Time = '" + ddlTripTime.SelectedItem + "' and TripSchedule.TripType = '" + ddlTripType.SelectedItem + "' and Bus.Bus# = '" + ddlBus.SelectedItem + "' order by Person.Group#";
SqlCommand cmd = new SqlCommand(strQuery);
TRIPTYPE = ddlTripType.SelectedItem.ToString();
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(strConnString);
SqlDataAdapter adapter = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
adapter.SelectCommand = cmd;
adapter.Fill(dt);
int dtr = 0;
dtr = dt.Rows.Count;
MGRchk = dtr;
if (dt.Rows.Count > 0)
{
Formats();
PrintTable(dt);
lblAct.Visible = false;
lblAgee.Visible = false;
lblBoats.Visible = false;
lblCells.Visible = false;
lblEmer.Visible = false;
lblFirst.Visible = false;
lblGroups.Visible = false;
lblInit.Visible = false;
lblLast.Visible = false;
txtAct.Visible = false;
ddlAgeR.Visible = false;
ddlBoat1.Visible = false;
txtCells.Visible = false;
txtEmer.Visible = false;
txtFirst.Visible = false;
txtGroups.Visible = false;
txtInit.Visible = false;
txtLast.Visible = false;
btnSubmitted.Visible = false;
}
else
{
lblAct.Visible = true;
lblAgee.Visible = true;
lblBoats.Visible = true;
lblCells.Visible = true;
lblEmer.Visible = true;
lblFirst.Visible = true;
lblGroups.Visible = true;
lblInit.Visible = true;
lblLast.Visible = true;
txtAct.Visible = true;
ddlAgeR.Visible = true;
ddlBoat1.Visible = true;
txtCells.Visible = true;
txtEmer.Visible = true;
txtFirst.Visible = true;
txtGroups.Visible = true;
txtInit.Visible = true;
txtLast.Visible = true;
btnSubmitted.Visible = true;
txtAct.Text = "";
txtCells.Text = "";
txtEmer.Text = "";
txtFirst.Text = "";
txtGroups.Text = "";
txtInit.Text = "";
txtLast.Text = "";
}
protected void AddNewCustomer(object sender, EventArgs e)
{
BindData();
switch(TRIP)
{
case "CANOE":
if(MGRchk < 24)
{
ValidatedAdd(sender, e)
break;
}
else
{
lblError1.Text = error1;
}
}
//etc
protected void ValidatedAdd(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 nGroup = ((TextBox)GridView1.FooterRow.FindControl("txtGroup")).Text;
string aboat = ((DropDownList)GridView1.FooterRow.FindControl("ddlBoat")).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);"
+ "insert into TripSchedule(TripType, PersonID, Time, Date, BusID) values (@Type, SCOPE_IDENTITY(), @Time, @Date, @Bus);" +
"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 INNER JOIN Bus on Bus.BusID = TripSchedule.BusID where TripSchedule.Date = @Date and " +
"TripSchedule.Time = @Time and TripSchedule.TripType = @Type and Bus.BusID = @Bus order by Person.Group#;");
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("@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.Parameters.AddWithValue("@Boat", aboat.ToString());
cmd.Parameters.AddWithValue("@Bus", ddlBus.SelectedItem.ToString());
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
答案 0 :(得分:1)
每次添加新客户时,都会在进行插入之前绑定GridView 。这种重新绑定每次都会清除你的价值观。
在AddNewCustomer
中删除该绑定(因为无论如何你再次绑定在ValidateAdd
中)或删除ValidateAdd
中的绑定并将AddNewCustomer
中的绑定移动到 插入后。