在下初学者 事有所急 看的头都大了 
来这了 以此形式撒分了错误提示:此 SqlParameterCollection 中未包含带有 ParameterName“packing”的 SqlParameter。
 显示再该句处MyCommand.Parameters[Cols[i - 1]].Value = ColValue;我的代码:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)

BindGridToSource();

TabletTypeIndex = new Hashtable();
TabletTypeIndex["片剂"] = 0;
TabletTypeIndex["注射剂"] = 1;
TabletTypeIndex["酊水油膏"] = 2;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
// 
// sqlConnection1
// 
this.sqlConnection1.ConnectionString = "workstation id=\"WWW-HZ0752-NET\";packet size=4096;integrated security=SSPI;data so" +
"urce=\".\";persist security info=False;initial catalog=my";
// 
// sqlCommand1
// 
this.sqlCommand1.CommandText = "SELECT tablet.* FROM tablet";
this.sqlCommand1.Connection = this.sqlConnection1;
this.Load += new System.EventHandler(this.Page_Load); }
#endregion public int GetTabletTypeIndex(string TabletTypeName)
{
if (TabletTypeIndex[TabletTypeName] != null)
return (int)TabletTypeIndex[TabletTypeName];
else
return 0; }
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindGridToSource();
} private void BindGridToSource()
{
SqlConnection MyConnection = new SqlConnection("server=(local);database=my;Trusted_Connection=yes");
SqlDataAdapter MyCommand = new SqlDataAdapter("SELECT 药品名称,规格,包装,剂型,单位,单价 FROM tablet", MyConnection);
DataSet ds = new DataSet();
MyCommand.Fill(ds, "tablet");
DataGrid1.DataSource = ds.Tables["tablet"].DefaultView;
DataGrid1.DataBind();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindGridToSource();
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection MyConnection = new SqlConnection("server=(local);database=my;Trusted_Connection=yes");

// 建立 UPDATE 命令字串
string UpdateCmd = "UPDATE tablet SET  规格=@Guige, 包装 =@packing," +
"剂型 =@TabletType, 单位 =@Danwei, 单价 =@Danjia  WHERE 药品名称 =@uid";

SqlCommand MyCommand = new SqlCommand(UpdateCmd, MyConnection);
// 设定 UPDATE 命令字串中各个参数的数据类型与长度
MyCommand.Parameters.Add(new SqlParameter("@uid", SqlDbType.NVarChar, 30));
//MyCommand.Parameters.Add(new SqlParameter("@TName", SqlDbType.NVarChar, 12));
MyCommand.Parameters.Add(new SqlParameter("@Guige", SqlDbType.NChar ,20));
MyCommand.Parameters.Add(new SqlParameter("@packing", SqlDbType.NVarChar, 20));
MyCommand.Parameters.Add(new SqlParameter("@TabletType", SqlDbType.NVarChar, 10));
MyCommand.Parameters.Add(new SqlParameter("@Danwei", SqlDbType.NChar,10 ));
MyCommand.Parameters.Add(new SqlParameter("@Danjia", SqlDbType.NChar ,20));

// 取得主索引键之值(此处为药品名称)
MyCommand.Parameters["@uid"].Value = DataGrid1.DataKeys[e.Item.ItemIndex];

string[] Cols = new string[] {"Guige", "packing", "TabletType", "Danwei", "Danjia"};
//string[] Cols = new string[] {"Name", "Gender", "Phone", "Salary", "Department"};

// 取得 TableCell 物件的数目,也就是每一数据列之储存格的数目(等於 DataGrid 伺服器控制项中数据行的数目)
int NumCols = e.Item.Cells.Count;
// 此回圈会将各字段(储存格)或控制项的数据指派给 UPDATE 命令中的参数。
// 略过第一栏与第二栏,因为第一栏是按钮,第二栏的身份证号码则已在之前取得。
for (int i = 2; i<=NumCols - 1; i++)
{
switch (i)
{

case 4: // 第 5 个字段(储存格)

// 由於第 6 个字段「剂型」是使用样板数据行中名称为 TabletTypeDropList
// 的下拉式清单来编辑,因此必须先使用 FindControl 方法
// 找到此下拉式清单, 并将使用者所选取的选项指派给参数 @BloodType 。
DropDownList TabletTypeDropList;
TabletTypeDropList = (DropDownList)e.Item.FindControl("TabletTypeDropList");
MyCommand.Parameters["@TabletType"].Value = TabletTypeDropList.SelectedItem.ToString(); break; default:

TextBox CurrentTextBox;
CurrentTextBox=(TextBox)e.Item.Cells[i].Controls[0];
string ColValue = CurrentTextBox.Text;

// 检查字段(储存格)的内容是否为 Null 值
if (ColValue == "")
{
Message.Text = "错误: 每一个字段都必须输入数据不允许 Null 值!";
return;
}
MyCommand.Parameters[Cols[i - 1]].Value = ColValue;
break;
}
}
// 开启连接
MyCommand.Connection.Open();
try
{
// 呼叫 ExecuteNonQuery() 方法以便针对数据来源执行 UPDATE 命令
MyCommand.ExecuteNonQuery();
//Message.Text = "<b>已更新数据纪录</b><br>"; // 完成更新作业后使数据行跳出编辑模式
DataGrid1.EditItemIndex = -1;
}
catch (SqlException Exp)
{
if (Exp.Number == 2627)
Message.Text = "错误: 具有相同主索引键的数据纪录已经存在。";
else
Message.Text = "错误: 无法更新数据纪录,请确定各字段是否都已正确输入。";

// 关闭连接
MyCommand.Connection.Close();
BindGridToSource();
}
} private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Response.BufferOutput =true;
Response.Redirect ("WebForm2.aspx");
} private void Button1_Click(object sender, System.EventArgs e)
{
Response.BufferOutput =true;
Response.Redirect ("addtab.aspx");
} private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection MyConnection = new SqlConnection("server=(local);database=my;Trusted_Connection=yes");
string DeleteCmd = "DELETE FROM tablet WHERE 药品名称 = @Uid";

SqlCommand MyCommand = new SqlCommand(DeleteCmd, MyConnection);
MyCommand.Parameters.Add(new SqlParameter("@Uid", SqlDbType.NVarChar,30));
MyCommand.Parameters["@Uid"].Value = DataGrid1.DataKeys[e.Item.ItemIndex];

MyCommand.Connection .Open ();
try
{
MyCommand.ExecuteNonQuery ();
Message.Text ="数据记录已经成功删除";
}
catch(SqlException)
{
Message.Text ="错误,无法删除数据记录";
}
            MyCommand.Connection .Close  ();
BindGridToSource();
}

解决方案 »

  1.   

    SqlDataAdapter就没有Parameters这个类,你要用参数的话可以在Adapter下的四个Command中设置参数
      

  2.   

    MyCommand.Parameters.Add(new SqlParameter("@packing", SqlDbType.NVarChar, 20));
    你只给包装添加参数了,却没给这个参数一个值,也就是MyCommand.Parameters.Add(new SqlParameter("@packing", SqlDbType.NVarChar, 20)).Value=???;
    其它参数都给值了,为什么这个不给?
      

  3.   

    MyCommand.Parameters.Add(new SqlParameter("@packing", SqlDbType.NVarChar, 20)).Value=???;
    其它参数都给值了,为什么这个不给?就是给uid设了参数了 :MyCommand.Parameters["@uid"].Value = DataGrid1.DataKeys[e.Item.ItemIndex];
    其他的和packing一样的写法啊 ?
    MyCommand.Parameters.Add(new SqlParameter("@TabletType", SqlDbType.NVarChar, 10));
    MyCommand.Parameters.Add(new SqlParameter("@Danwei", SqlDbType.NChar,10 ));
    MyCommand.Parameters.Add(new SqlParameter("@Danjia", SqlDbType.NChar ,20));