Page view counter

Urgent Help needed: How to INSERT into a field having "UniqueIdentifier" data type

Last post 01-10-2008 3:23 PM by JKC. 5 replies.

Sort Posts:

  • Urgent Help needed: How to INSERT into a field having "UniqueIdentifier" data type

    01-08-2008, 6:37 AM
    • Loading...
    • jadoon88
    • Joined on 12-09-2007, 9:41 AM
    • Pakistan
    • Posts 71

    I am new at ASP.net and I am having problems inserting data using C# in ASP.net

    I have created a table named "Profile" in the MS sql server database named "MyDataBase". There is a field named "ID" that has data type 'uniqueidentifier'.

    I am confused how to INSERT data into this data field. I have used MS Access and MYSQL in which there is an option of auto increment so we don't a unique identifier for each record.

    Please tell me what can I do to  If I want to have a uniqueidentifier for each new record I INSERT in the "Profile" table of MS sql server database.

    While trying to insert, I get following errors

    Cannot insert the value NULL into column 'ID'

    and I don't know how I can insert something in this field that is of value type unique identifier.

    Please help me I will be very thankfull of you.

    Umair Khan Jadoon
    Blog: www.technobuddy.net
  • Re: Urgent Help needed: How to INSERT into a field having "UniqueIdentifier" data type

    01-08-2008, 6:51 AM
    Answer

    jadoon88:

    I am new at ASP.net and I am having problems inserting data using C# in ASP.net

    I have created a table named "Profile" in the MS sql server database named "MyDataBase". There is a field named "ID" that has data type 'uniqueidentifier'.

    I am confused how to INSERT data into this data field. I have used MS Access and MYSQL in which there is an option of auto increment so we don't a unique identifier for each record.

    Please tell me what can I do to  If I want to have a uniqueidentifier for each new record I INSERT in the "Profile" table of MS sql server database.

    While trying to insert, I get following errors

    Cannot insert the value NULL into column 'ID'

    and I don't know how I can insert something in this field that is of value type unique identifier.

    Please help me I will be very thankfull of you.

    One can generate a uniqueidentier (also known as a "Guid") programmatically.

    This line of code...

    Guid myNewId = Guid.NewGuid();

    ...will put a new Guid (also known as a "uniqueidentifier") into the variable myNewId.

    You can use myNewId for the "ID" value in your insert and that will work fine.

    HTH.

    Thank you.

    -- Mark Kamoski

  • Re: Urgent Help needed: How to INSERT into a field having "UniqueIdentifier" data type

    01-08-2008, 6:57 AM
    Answer

    Use Guid.NewGuid().ToString() function to create the GUID and use this to insert it into table.

    See the below code for example: 


    String sql = "INSERT INTO users (TestColumn) VALUES (@ID)";

    SqlCommand cmd = new SqlCommand(sql, cn);

    cmd.Parameters.Add("@ID", SqlDbType.UniqueIdentifier);

    String GuidVal = Guid.NewGuid().ToString();

    cmd.Parameters[0].Value = new SqlGuid(GuidVal);

    cn.Open();

    cmd.ExecuteNonQuery();

    cn.Close();

    Shah Dharnendra G
    Sr.Analyst Programmer,
    GTL-Ahmedabad
  • Re: Urgent Help needed: How to INSERT into a field having "UniqueIdentifier" data type

    01-08-2008, 7:29 AM

    for SQL Server 2005, using Managment Studio Express:

    change or add a column type of int in the column properties, under the 'Identity Specification' select yes. This will create a identity column, it auto inserts when a new record is added

    in SQL Server 2000, set the column as int and set Identity to Yes, which also creates an identity column (its called autonumber in MS Access i think?)

     

  • Re: Urgent Help needed: How to INSERT into a field having "UniqueIdentifier" data type

    01-10-2008, 3:06 PM
    • Loading...
    • jadoon88
    • Joined on 12-09-2007, 9:41 AM
    • Pakistan
    • Posts 71

    I have created the database in VisualStudio and inside visual studio, its not allowing me to change the 'Identity Specification' to yes. I hv installed MS SQL Server Management Studio Express and tried to open the database frm App_Data folder of this application but it gives the followign error :S

    Umair Khan Jadoon
    Blog: www.technobuddy.net
  • Re: Urgent Help needed: How to INSERT into a field having "UniqueIdentifier" data type

    01-10-2008, 3:23 PM
    • Loading...
    • JKC
    • Joined on 09-19-2007, 9:46 AM
    • Posts 251

    You need to ATTACH the MDF into SQL studio express...

     RIGHT CLICK on Databases on the left in the SQL studio express, look for ATTACH. This will place the MDF in the express manager.
     

    Thanks, JKC

    - Be a good developer, follow the W3C whenever you can.
Page 1 of 1 (6 items)
Microsoft Communities