Using GUID and SQL in code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc

    Using GUID and SQL in code

    Hi, I don't get it I cannot get this to work, can somebody give me a hint
    Table1 contains a field Id which is a GUID as primary key and DATA a string,
    I want to insert a new row but it does not work.


    void gv_RowCommand(O bject sender, GridViewCommand EventArgs e)
    {
    ClientScript.Re gisterStartupSc ript(GetType(), "MyAlert2",
    "alert('Command =" + e.CommandName + "');", true);
    if (e.CommandName == "New")
    {
    SqlCommand cmd = new SqlCommand();

    //First problem How do I make a GUID?
    SqlGuid g = new SqlGuid("3AAAAA AA-BBBB-CCCC-DDDD-2EEEEEEEEEEE");
    //Second problem, how do I use the GUID in the Insert
    cmd.CommandText = "INSERT INTO dbo.Table1
    Values("+g.ToSt ring()+",'tst') "; //?????


    cmd.CommandType = CommandType.Tex t ;
    SqlConnection sqlConnection1 = new
    SqlConnection(S qlDataSource1.C onnectionString );
    cmd.Connection = sqlConnection1;
    sqlConnection1. Open();
    cmd.ExecuteNonQ uery();
    sqlConnection1. Close();
    GridView1.DataB ind();
    }
    }


  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: Using GUID and SQL in code

    SqlGuid guid = new SqlGuid(Guid.Ne wGuid());
    string str = "some string here";

    string sql = "INSERT INTO TABLE VALUES (@guid,@str)";

    SqlCommand cmd = new SqlCommand();
    cmd.Parameters. AddWithValue("@ guid", guid);
    cmd.Parameters. AddWithValue("@ str", str);


    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog


    or just read it:


    *************** *************** **************
    | Think outside the box! |
    *************** *************** **************
    "Marc" <noreply@chello .nlwrote in message
    news:%23NlqEfJ$ IHA.1016@TK2MSF TNGP03.phx.gbl. ..
    Hi, I don't get it I cannot get this to work, can somebody give me a hint
    Table1 contains a field Id which is a GUID as primary key and DATA a
    string, I want to insert a new row but it does not work.
    >
    >
    void gv_RowCommand(O bject sender, GridViewCommand EventArgs e)
    {
    ClientScript.Re gisterStartupSc ript(GetType(), "MyAlert2",
    "alert('Command =" + e.CommandName + "');", true);
    if (e.CommandName == "New")
    {
    SqlCommand cmd = new SqlCommand();
    >
    //First problem How do I make a GUID?
    SqlGuid g = new SqlGuid("3AAAAA AA-BBBB-CCCC-DDDD-2EEEEEEEEEEE");
    //Second problem, how do I use the GUID in the Insert
    cmd.CommandText = "INSERT INTO dbo.Table1
    Values("+g.ToSt ring()+",'tst') "; //?????
    >
    >
    cmd.CommandType = CommandType.Tex t ;
    SqlConnection sqlConnection1 = new
    SqlConnection(S qlDataSource1.C onnectionString );
    cmd.Connection = sqlConnection1;
    sqlConnection1. Open();
    cmd.ExecuteNonQ uery();
    sqlConnection1. Close();
    GridView1.DataB ind();
    }
    }
    >

    Comment

    Working...