2012-05-02 9 views
1

一意の識別子であるIDに基づいて、gridviewとデータベースから行を削除する必要があります。 以下の私のコードでは、エラーメッセージ "Unrecognized Guid format"が表示されます。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

私はまだ文字列IDのようなguid.tryparse使用した後、同じエラーが出る= GridView1.Rows [e.RowIndex] .Cells [0] .textセクションを。 ガイドmyid; Guid.TryParse(id、out myid); – newuser1555