PDA

View Full Version : مشکل در عوض کردن server در یک report در 2005



combo_ci
یک شنبه 01 اردیبهشت 1387, 11:00 صبح
سلام
من توی vs 2003 بدون هیچ مشکلی سرور یک report رو با کد

Dim crtableLogoninfos As New CrystalDecisions.Shared.TableLogOnInfos
Dim crtableLogoninfo As New CrystalDecisions.Shared.TableLogOnInfo
Dim crConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo
crConnectionInfo.ServerName = ServerName
crConnectionInfo.UserID = UserName
crConnectionInfo.Password = Pass
Dim CrTable As CrystalDecisions.CrystalReports.Engine.Table
For Each CrTable In rp.Database.Tables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
Next

عوض میکردم

اما تو vs 2005 وقتی این کد رو اجرا میکنم موقع نمایش report دوباره پارامتر هایی که با setParameterValue مقدار دادم رو ازم میخواد

من فکر کنم این کد توی vs 2005 پارامتر های همراه report رو پاک میکنه ...کسی میتونه راهنماییم کنه؟

MORTEZA66
دوشنبه 02 اردیبهشت 1387, 09:08 صبح
اینو می گم یه توضیح کلی که شاید مورد استفاده دیگران هم با شه بعد هم جواب شما رو می دم

MORTEZA66
دوشنبه 02 اردیبهشت 1387, 09:13 صبح
اول از همه دو تا متغیر زیر رو برای کار با دیتاست و دستورات SQL تعریف کنید و یه کم مقدار دهی کنید

DIM dap1 As New SqlClient.SqlDataAdapter
DIM dset1 As New DataSet
dap1.SelectCommand = New SqlClient.SqlCommand
dap1.SelectCommand.Connection = con ‘Connection name


یک شی یا هر چی از نوع گزارشتون تعریف می کنید

Dim rpt As New kolRpt
بعد برای اینکه موقع نمایش گزارش کادر User و Pass نیاد خط زیر رو که فکر می کنم همش واضح باشه می نویسید

rpt.DataSourceConnections.Item(0).SetConnection(se rver, database, username, pass)
بعد دستور SQL رو می سازید

sqltext = "select name,family,fathername from main_tlb Where id=1"
بعد متغیرهایی که در ابتدا برای کار با دیتاست و دستورات SQL ساختید رو به صورت زیر مقدار دهی می کنید

dset1.Dispose()
dset1 = New DataSet
dap1.SelectCommand.CommandText = sqltext
dset1.Clear()
dap1.Fill(dset1, "main_tlb")
rpt.SetDataSource(dset1)
بعد هم RPT رو که در واقع یک گزارش است به هر گزارشی که دلتون می خواد برای دیدن پیش نمایش نسبت می دید


frm.viewrep1.ReportSource = rpt
frm.Show()

MORTEZA66
دوشنبه 02 اردیبهشت 1387, 09:16 صبح
اما برای عوض کردن سرور هم می تونید در خط زیر به جای سرور هر نامی سروری که می خواید قرار بدید


rpt.DataSourceConnections.Item(0).SetConnection(se rver, database, username, pass)

MORTEZA66
دوشنبه 02 اردیبهشت 1387, 09:20 صبح
امیدوارم مشکلتون حل شده باشه

shahram197070
سه شنبه 03 اردیبهشت 1387, 13:11 عصر
با سلام
با کد زیر مشکل من حل شد ببین به درد می خوره !!!






PublicSub ShowRpt()
Dim pvCollection AsNew CrystalDecisions.Shared.ParameterValues()
Dim tbCurrent As CrystalDecisions.CrystalReports.Engine.Table
Dim tliCurrent AsNew CrystalDecisions.Shared.TableLogOnInfo
Dim ConnectInfo AsNew CrystalDecisions.Shared.ConnectionInfo
Dim sSections As Sections


ServerName = "Your servername here"
UserID = "Your database user name here"
Password = "Your database user password here"
DatabaseName = "Your database name here"

With ConnectInfo
.ServerName = ServerName
.UserID = UserID
.Password = Password
.DatabaseName = DatabaseName
EndWith
Dim rpt AsNew ReportDocument
Dim subRpt AsNew ReportDocument
Dim subRptObj As SubreportObject
Dim RptObjs As ReportObjects
Try
Dim strPathApp AsString = Application.StartupPath
rpt.Load(strPathApp & mstrPathRpt & rptInfo.RptName)

'Resetting connection information for each table that your report will be using. ForEach tbCurrent In rpt.Database.Tables
tliCurrent = tbCurrent.LogOnInfo
tliCurrent.ConnectionInfo = ConnectInfo
tbCurrent.ApplyLogOnInfo(tliCurrent)

'Important 1: This code below very important, it will help your program re-setting location for connection.
If (tbCurrent.TestConnectivity() = False) Then
Return
Else
If (tbCurrent.Location.IndexOf(".") > 0) Then
tbCurrent.Location = tbCurrent.Location.Substring(tbCurrent.Location.La stIndexOf(".") + 1)
Else
tbCurrent.Location = tbCurrent.Location
EndIf
EndIf
'End - Important 1
Next tbCurrent

'Loop all sections inside your report, find the subreport and resetting connection information for each report inside too
sSections = rpt.ReportDefinition.Sections
ForEach sSection As Section In sSections
RptObjs = sSection.ReportObjects
ForEach RptObj As ReportObject In RptObjs
If RptObj.Kind = CrystalDecisions.Shared.ReportObjectKind.Subreport Object Then
subRptObj = CType(RptObj, SubreportObject)
subRpt = subRptObj.OpenSubreport(subRptObj.SubreportName)
ForEach tbCurrent In subRpt.Database.Tables
tliCurrent = tbCurrent.LogOnInfo
tliCurrent.ConnectionInfo = ConnectInfo
tbCurrent.ApplyLogOnInfo(tliCurrent)
If (tbCurrent.TestConnectivity() = False) Then
Return
Else
If (tbCurrent.Location.IndexOf(".") > 0) Then
tbCurrent.Location = tbCurrent.Location.Substring(tbCurrent.Location.La stIndexOf(".") + 1)
Else
tbCurrent.Location = tbCurrent.Location
EndIf
EndIf
Next tbCurrent
EndIf
Next RptObj
Next sSection
rpv.ReportSource = rpt
Catch ex As Exception
MsgBox(ex.message, MsgBoxStyle.Critical, "")
EndTry
EndSub


rpv هم همون کنترل crystalreportviewer هسش

بر گرفته از لینک زیر :
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=805447&SiteID=1

MORTEZA66
چهارشنبه 04 اردیبهشت 1387, 09:38 صبح
کدهای شما هم جالب بود. ولی من فکر کنم کدهایی که من نوشتم هم آسونتر بود و هم فهم شون راحت تر بود به هر حال زحمت کشیدید دستتون درد نکنه