下面是问题所在相关代码,请各位大侠帮分析分析!
protected void btnEdit_Click( object sender, EventArgs e)
    {
        int shipperID = GetSelectedRecord();
        string cmd = @"Update Shippers set CompanyName = '" +
            this.txtName.Text +
            "', Phone ='" + this.txtPhone.Text + "' where ShipperID = " + shipperID;
        UpdateDB(cmd);
        PopulateGrid();
 
    }
         protected int GetSelectedRecord()
        {
            int shipperID = -1;
            int index = GridView1.SelectedIndex;
            if (index != -1)
            {
                DataKey key = GridView1.DataKeys[index];
                shipperID = (int)key.Value;
                return shipperID;
            }
        }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string cmd = @"delete from Shippers where ShipperID =" + GetSelectedRecord();        UpdateDB(cmd);
        PopulateGrid();
    }

解决方案 »

  1.   

       protected   int   GetSelectedRecord() 
                    { 
                            int   shipperID   =   -1; 
                            int   index   =   GridView1.SelectedIndex; 
                            if   (index   !=   -1) 
                            { 
                                    DataKey   key   =   GridView1.DataKeys[index]; 
                                    shipperID   =   (int)key.Value; 
                                     
                            } 
    return   shipperID;
                    } 
    改这样
      

  2.   

    GetSelectedRecord() 函数里面那个if判断如果是假的话就没返回值了 ,所以报错
    可以参考1楼的代码改一下