2014-03-03 52 views
1

我想顯示來自兩個表的gridview的細節和登錄名是樹形結構,管理,經理,用戶多部分標識符「Maintenanceinfo.userid」找不到

一個表Maintenanceinfo和另一種是的UserDetails

下面是我的代碼:

Private Sub fillgrid() 
     dt1 = Now.Year 

     If Convert.ToString(Session("userType")).ToLower() = "admin" Then 
      cmd.CommandText = "select sno, (SELECT UserName FROM UserDetails WHERE userid=Maintenanceinfo.userid) AS userid, Maintenance, January, February, March, April, May, June, July, August, September, October, November, December from Maintenanceinfo where year = " & dt1 & "" 
     ElseIf Convert.ToString(Session("userType")).ToLower() = "manager" Then 
      cmd.CommandText = "select sno, (SELECT UserName FROM UserDetails WHERE userid=Maintenanceinfo.userid) AS userid, Maintenance, January, February, March, April, May, June, July, August, September, October, November, December from Maintenanceinfo a,UserDetails b where a.userid=b.userid and b.Managerid= " & Session("userId") & " and year = " & dt1 & " " 
     Else 
      cmd.CommandText = "select sno, (SELECT UserName FROM UserDetails WHERE userid=Maintenanceinfo.userid) AS userid, Maintenance, January, February, March, April, May, June, July, August, September, October, November, December from Maintenanceinfo with (nolock) where userid= " & Session("userId") & " and year = " & dt1 & "" 
     End If 

     da = New SqlDataAdapter(cmd.CommandText, Con) 
     da.Fill(ds, "info") 
     Me.GridView1.DataSource = ds.Tables("info") 

     GridView1.DataBind() 
     ' cmd.CommandText = "select isnull(count(*),0) from DM_Memberinfo m inner join DM_Dln d on m.memberid=d.dlnmemid where d.memberid=" & Session("uname") & " " 
    End Sub 

代碼工作時試圖登錄爲「admin」和「用戶」,而是試圖登錄爲「經理」,當它不能正常工作很好,稱「Maintenanceinfo.userid」找不到

感激,如果有人可以幫助我

謝謝

+0

你有一個查詢低了下去,它使用'INNER JOIN',所以你顯然意識到它們的存在,那麼,爲什麼您使用的是舊式' '加入你的第二個查詢? SQL風格已經存在超過20年的ANSI風格的聯接!另外,如果那些'Session'值沒有被清理過,* hello SQL注入!*您可能想使用參數來查看。 –

+0

是的,我正在使用新舊代碼混合,內部連接代碼是由我沒有ide的朋友之一編寫的,無論如何這些問題現在已經解決了。謝謝 – user3373775

回答

0

對於管理者部分已定義的別名Maintenanceinfo表。所以,你應該使用或丟棄別名:

select sno, (SELECT UserName FROM UserDetails WHERE userid=a.userid) 
AS userid, Maintenance, January, February, March, April, May, June, July, August, 
September, October, November, December from Maintenanceinfo a,UserDetails b 
where a.userid=b.userid and b.Managerid= " & Session("userId") & " 
and year = " & dt1 & " " 
+0

非常感謝,代碼完美無缺 – user3373775