2012-07-24 121 views
1

我有表如何獲取用戶名到MVC3中的資源名稱?

Resource 

ResourceId | ResourceName |username | password  
    1  | raghu  | aaaa | ****** 
    2  | anil  | bbbb | ******   

BugHistory 

BugHisoryId | FixedByID | AssignedByID 
    1  | 2  | 1 
    2  | 1  | 2 

衛生組織的登錄名同名的用戶名來獲取資源名稱。 FixedByIdforeign key(FixedById) reference Resource(ResourceId)

我控制器代碼

public ActionResult BugHistory(BugTracker_DataHelper bugdatahelepr, string loginname, string EmployeName) 
{ 
    Session["UserName"] = "aaaa"; 
    loginname = Session["UserName"].ToString(); 
    //bugdatahelepr.Username = loginname.ToString(); 
    //var username = bugdatahelepr.Username; 

    SqlConnection connection = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MvcBugTracker;Data Source=SSDEV6\SQLEXPRESS"); 
    connection.Open(); 
    SqlCommand cmd = new SqlCommand("select ResourceName from Resources where UserName =" + loginname, connection);  

    SqlDataReader dr = cmd.ExecuteReader(); 

    if (dr.Read()) 
    { 
     bugdatahelepr.FixedByID = Convert.ToInt16(dr["ResourceName"]); 
     //updatemodel.ProjectId = Convert.ToInt16(dr["ProjectId"]); 
    } 
    else 
    { 
     dr.Close(); 
    } 
    dr.Close(); 
    connection.Close(); 

    //ViewBag.BugHistoryId = new SelectList(ToStatusDropdown(), "BugHistoryId", "ToStatus"); 
    //ViewData.AssignedToID=new SelectList() 

    return View(); 
} 

我的視圖代碼

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Gridview_BugTracker.Models.BugTracker_DataHelper>" %> 

<!DOCTYPE html> 

<html> 
<head runat="server"> 
    <title>BugHistory</title> 
</head> 
<body> 
    <div> 

    <%: ViewBag.Title = "BugHistory"%> 
    <% using (Html.BeginForm()) 
    { %>  
     <%:Html.ValidationSummary(true)%>  

     <fieldset> 
     <legend>BugHistory</legend> 

     <div class="editor-label"> 
      <%:Html.LabelFor(model => model.FixedByID)%> 
     </div> 
     <div class="editor-field"> 
      <%:Html.LabelFor(model => model.FixedByID)%> 
      <%:Html.ValidationMessageFor(model => model.FixedByID)%> 
     </div> 
     <div class="editor-label"> 
     <%:Html.LabelFor(model => Model.Resolution)%> 
    </div> 
    <div class="editor-field"> 
     <%:Html.EditorFor(model => model.Resolution)%> 
     <%:Html.ValidationMessageFor(model => model.Resolution)%> 
    </div> 
     <%: Html.DropDownList("BugHistoryId", (SelectList)ViewBag.BugHistoryId, "--Select Project--")%> 
     <%: Html.ValidationMessage("BugHistoryId")%> 
     </fieldset> 

     <% }%> 
     <form action="AssignProject.aspx" method="post"> 
      <p> <input type="submit" value="insert" /></p> 
      </form>   


    </div> 
</body> 
</html> 

我得到錯誤

Invalid column name 'aaaa'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'raghu'. 

Source Error: 


Line 270:   SqlCommand cmd = new SqlCommand("select ResourceName from Resources where UserName =" + loginname, connection);  
Line 271:   
Line 272:   SqlDataReader dr = cmd.ExecuteReader(); 
Line 273:    
Line 274:   if (dr.Read()) 


Source File:  C:\Raghu\Gridview_BugTracker\Gridview_BugTracker\Controllers\ProjectsController.cs Line: 272 

當我登錄頁面用戶名aaaa獲取資源名稱。任何人都可以幫我做到這一點?

IN veiw page i Diplay like this 

FixedBYID -----------raghu <---Lable in disabale 

AssignedBY ID-------- anil <----dropdownlist in disable 
+0

從錯誤中看,問題是它試圖在數據庫中找到列「aaaa」,而不是列「username」。 – 2012-07-24 07:23:59

+0

@GarrettFogerlie ...這是我的查詢「從資源中選擇ResourceName,其中UserName =」+ loginname, – 2012-07-24 07:25:15

+0

@GarrettFogerlie ..我在數據庫中獲得用戶名相同,即使得到這種類型的錯誤來了......我該怎麼做.. – 2012-07-24 07:28:40

回答

2

在這段代碼中你應該改進一些東西。首先,總是(並且我的意思是總是)在using聲明中包裝連接和數據收集器。

二,做不是連接字符串來建立SQL查詢。這會爲您打開SQL插入攻擊。相反,你應該使用參數。

現在,對於您所看到的錯誤。您只需追加的用戶名查詢,這將使您的查詢是這樣的:

select ResourceName from Resources where UserName =aaaa 

用戶名沒有報價,這意味着數據庫服務器將嘗試尋找一個名爲「AAAA」列。這正是你所看到的錯誤信息。

數據訪問代碼應該是這個樣子:

using(var connection = new SqlConnection(@".... your connection string here ...")) 
{ 
    connection.Open(); 

    var cmd = new SqlCommand("select ResourceName from Resources where UserName [email protected]", connection);  
    cmd.Parameters.Add(name, DbType.String).Value = loginName; 

    using(var reader = cmd.ExecuteReader()) 
    { 
    if (dr.Read()) 
    { 
     // read your data here 
    } 
    } 
} 
+0

謝謝你很多...我得到了輸出... – 2012-07-24 08:08:30

0

你,因爲遇到了錯誤:

  • 你不使用引號 - 在SQL字符串常量必須''
  • 你做錯了:你應該使用參數,而不是連接字符串