Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 3.22 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Retrieving single value from query
  2. public int GetUserRole(string CUSER)
  3. {
  4.     try
  5.     {
  6.         SQLCON = new SqlConnection(connectionString);
  7.         SQLCON.Open();
  8.         SQLCommand = new SqlCommand();
  9.         SQLCommand.CommandType = CommandType.Text;
  10.         SQLCommand.Parameters.Add("USUsername", SqlDbType.VarChar).Value = CUSER;
  11.         SQLCommand.CommandText = "SELECT USRole FROM tblUser WHERE USUsername = CUSER";
  12.         Int32 USRole = (Int32) SQLCommand.ExecuteScalar();
  13.  
  14.         return USRole;
  15.  
  16.     }
  17.     catch
  18.     {
  19.         HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx", false);
  20.         return 0;
  21.     }
  22. }
  23.        
  24. ALTER PROCEDURE [dbo].[spGetUserRole]
  25. -- Add the parameters for the stored procedure here
  26.     @username VARCHAR(50)
  27. AS
  28. BEGIN
  29. -- Declare the return variable here
  30. DECLARE @USRole as int
  31.  
  32. -- Add the T-SQL statements to compute the return value here
  33. SELECT @USRole = tblUser.USRole FROM tblUser WHERE USUsername = @username
  34.  
  35. -- Return the result of the function
  36. RETURN @USRole  
  37. END
  38.        
  39. public int GetUserRole(string CUSER)
  40. {
  41.     try
  42.     {
  43.         SQLCON = new SqlConnection(connectionString);
  44.         SQLCON.Open();
  45.         SQLCommand = new SqlCommand();
  46.         SQLCommand.CommandType = CommandType.Text;
  47.         SQLCommand.Parameters.Add("USUsername", SqlDbType.VarChar).Value = CUSER;
  48.         SQLCommand.CommandText = "SELECT USRole FROM tblUser WHERE USUsername = @USUsername";
  49.         Int32 USRole = (Int32) SQLCommand.ExecuteScalar();
  50.  
  51.         return USRole;
  52.  
  53.     }
  54.     catch (Exception)
  55.     {
  56.         HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx", false);
  57.         return 0;
  58.     }
  59. }
  60.        
  61. ALTER PROCEDURE [dbo].[spGetUserRole]
  62. -- Add the parameters for the stored procedure here
  63. @USUsername VARCHAR(50)
  64.  
  65. AS
  66. BEGIN
  67.  
  68. -- Add the T-SQL statements to compute the return value here
  69. SELECT tblUser.USRole FROM tblUser WHERE USUsername = @USUsername
  70.  
  71. END
  72.        
  73. "SELECT USRole FROM tblUser WHERE USUsername = CUSER"
  74.        
  75. SQLCommand.Parameters.Add("@USUsername", SqlDbType.VarChar).Value = CUSER;
  76. "SELECT USRole FROM tblUser WHERE USUsername = @USUsername"
  77.        
  78. ALTER PROCEDURE [dbo].[spGetUserRole]
  79. -- Add the parameters for the stored procedure here
  80. @USUsername VARCHAR(50)
  81.  
  82. AS
  83. BEGIN
  84.  
  85. -- Add the T-SQL statements to compute the return value here
  86. Select tblUser.USRole
  87. FROM tblUser
  88. WHERE USUsername = @USUsername
  89.  
  90. END
  91.        
  92. public int GetUserRole(string CUSER)
  93.     {
  94.         try
  95.         {
  96.             SQLCON = new SqlConnection(connectionString);
  97.             SQLCON.Open();
  98.             SQLCommand = new SqlCommand();
  99.             SQLCommand.CommandType = CommandType.Text;
  100.             SQLCommand.CommandText = "SELECT USRole FROM tblUser WHERE USUsername = @USUsername  ";
  101.             SQLCommand.Parameters.Add("USUsername", SqlDbType.VarChar).Value = CUSER;
  102.  
  103.             Int32 USRole = (Int32) SQLCommand.ExecuteScalar();
  104.  
  105.             return USRole;
  106.  
  107.         }
  108.         catch (Exception)
  109.         {
  110.             HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx", false);
  111.             return 0;
  112.         }
  113.         finally { close connection here.. }
  114.  
  115.     }
  116.        
  117. SqlParameter p = cmd.Parameters.Add("@USRole", SqlDbType.Int);
  118. p.Direction = ParameterDirection.ReturnValue;
  119. cmd.ExecuteNonQuery();
  120. int returnvalue = (int)cmd.Parameters["@USRole"].Value;