I have i question. I dont know how to get uniqueidentifier value from database and display into my page. before this, i just know to to get int or string value only. This is sample:
Dim selectDeptHeadNameSql As String = "SELECT DeptHeadName FROM ApprovalLoop WHERE DeptName=@DeptName"
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand3 As New SqlCommand(selectDeptHeadNameSql, myConnection)
myCommand3.Parameters.AddWithValue("@DeptName", TxtDept.Text.Trim())
Dim DeptHeadName As String = DirectCast(myCommand3.ExecuteScalar(), String)
myCommand3.ExecuteNonQuery()
myConnection.Close()
End Using
How to changed this code to get DeptHeadID from database. DeptHeadID is uniqeidentifier.
create PROCEDURE GetDeptHeadId
(@DeptName varchar(50),
@DeptHeadId UniqueIdentifier out)
AS
BEGIN
select @DeptHeadId=DeptHeadId from ApprovalLoop where deptname = @DeptName
END
GO
code behind
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["tempConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("GetDeptHeadId",con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@DeptName", "computer");
p1.SqlDbType = SqlDbType.VarChar;
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("@DeptHeadId",SqlDbType.UniqueIdentifier);
p2.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p2);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Guid DeptHeadId = (Guid)p2.Value;
munirah-mali...
Member
83 Points
111 Posts
How to get uniqueidentifier value from database?
May 09, 2012 06:48 AM|LINK
Hi all,
I have i question. I dont know how to get uniqueidentifier value from database and display into my page. before this, i just know to to get int or string value only. This is sample:
Dim selectDeptHeadNameSql As String = "SELECT DeptHeadName FROM ApprovalLoop WHERE DeptName=@DeptName" Using myConnection As New SqlConnection(connectionString) myConnection.Open() Dim myCommand3 As New SqlCommand(selectDeptHeadNameSql, myConnection) myCommand3.Parameters.AddWithValue("@DeptName", TxtDept.Text.Trim()) Dim DeptHeadName As String = DirectCast(myCommand3.ExecuteScalar(), String) myCommand3.ExecuteNonQuery() myConnection.Close() End UsingHow to changed this code to get DeptHeadID from database. DeptHeadID is uniqeidentifier.
mahedee
Member
450 Points
116 Posts
Re: How to get uniqueidentifier value from database?
May 09, 2012 08:52 AM|LINK
Try with this.
DeptHead = (Guid)reader["DeptHeadName "];
Mahedee
Blog: http://mahedee.blogspot.com
tusharrs
Contributor
3230 Points
668 Posts
Re: How to get uniqueidentifier value from database?
May 09, 2012 09:00 AM|LINK
Guid DeptHeadId = (Guid)myCommand3.ExecuteScalar();
you can also go for stored procedure
stored proceudre
code behind
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["tempConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand("GetDeptHeadId",con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter p1 = new SqlParameter("@DeptName", "computer"); p1.SqlDbType = SqlDbType.VarChar; cmd.Parameters.Add(p1); SqlParameter p2 = new SqlParameter("@DeptHeadId",SqlDbType.UniqueIdentifier); p2.Direction = ParameterDirection.Output; cmd.Parameters.Add(p2); con.Open(); cmd.ExecuteNonQuery(); con.Close(); Guid DeptHeadId = (Guid)p2.Value;( Mark as Answer if it helps you out )
View my Blog