How to use Guid datatype in C#

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)
S1='Test1;
Else if (i==1)
S1='Test2;

Thanks in advance
LVL 1
dshi15Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_davisCommented:
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
0
dshi15Author Commented:
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?

Thanks,
0
p_davisCommented:
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();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.