2012-05-02 26 views
1

我需要從gridview和數據庫中刪除一行,這是基於它的唯一標識符的id。 在我的代碼下面我得到錯誤味精「無法識別的Guid格式」。任何幫助解決它?如何處理asp.net中的唯一標識符參數

  protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 

     string id = GridView1.Rows[e.RowIndex].Cells[0].Text; 

     SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 
     SqlCommand command = new SqlCommand("Delete", conn); 
     command.CommandType = CommandType.StoredProcedure; 

     command.Parameters.Add("@ID", SqlDbType.UniqueIdentifier); 
     command.Parameters["@ID"].Value = new System.Guid(id); 
     command.Connection.Open(); 
     command.ExecuteNonQuery(); 

     DataSet ds = new DataSet(); 
     SqlDataAdapter da = new SqlDataAdapter(command); 
     da.Fill(ds); 

     GridView1.DataSource = ds; 
     GridView1.DataBind(); 
     command.Connection.Close(); 

    } 

回答

0

試試這個:

Guid theGuid = new Guid(); 
System.Data.SqlTypes.SqlGuid.Parse(theGuid.ToString()); 

Adding a Guid Parameter into SqlCommand

+0

我試過你的,但沒有運氣仍然是一樣的錯誤msg – newuser1555

+0

string id = GridView1.Rows [e.RowIndex] .Cells [0] .Text; Guid myid = new Guid(id); System.Data.SqlTypes.SqlGuid theSqlGuid = new System.Data.SqlTypes.SqlGuid(myid); – newuser1555

0

對你的id字符串使用Guid.TryParse,那麼你就會知道發生了什麼。

+0

我仍然得到同樣的錯誤使用guid.tryparse像字符串ID後= GridView1.Rows [e.RowIndex] .Cells [0]。文本; Guid myid; Guid.TryParse(id,out myid); – newuser1555