How to use Guid datatype in C#

Posted on 2013-10-21
Medium Priority
Last Modified: 2013-10-29
Hi Expert,

I like to insert data to a column which are Guid datatype.

I like to insert two records

@p_Student_ID = new Guid("1A1A1A1A-AD43-499E-839F-32E31A1A1A1A")
@p_Student_ID = new Guid("1B1B1B1B-AD43-499E-839F-32E31B1B1B1B")

Can I declare a variable and assign a value later like string?

String S1="";

If (i==0)
Else if (i==1)

Thanks in advance
Question by:dshi15
  • 2
LVL 22

Expert Comment

ID: 39589022
if its local variable for a method, you would need to initialize it but if it is a class member you could assign a value at any time.

you can generally assign values at any given time but you cannot access before a value is assigned...
im not sure if i have understood your question so if you need more information, just let me know

Author Comment

ID: 39589684
It is class member, but how I can declare it and assign value in the method,

Public Class ABC
Guid  Student_ID =New Guid("");

Public void DEF()

for (int i = 0; i <= 1; i++)

     If (i==0)
         Student_ID =new Guid("1A1A1A1A-AD43-499E-839F-32E31A1A1A1A")    
     Else if (i==1)
         Student_ID =new Guid("1B1B1B1B-AD43-499E-839F-32E31B1B1B1B")

     other code here

        cmd.Parameters.AddWithValue("@p_Student_ID", Student_ID);

the syntax isn't correct, what is correct syntax to declare this Guid variable and assign these value to it?

LVL 22

Accepted Solution

p_davis earned 2000 total points
ID: 39590763
if you want to do it dynamically just use the static method

Guid Student_ID = Guid.NewGuid();

you can ToString() it to use for display if that is what you need

string str = Student_ID.ToString();

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question