Visual Studio Embedded Crystal Report Keeps Prompting Database Login? - vb.net

I'm using visual studio 2005 to develop a form with a combobox passing a value into the parameter of an embedded crystal report. I'm trying to figure out why it keeps prompting me for a database login every single time you try to run the report with a different combobox selection.
Here is my code:
private Sub Form1_load...
Dim ConnName As String
Dim ServerName As String
Dim DBName As String
Dim user As String
Dim pass As String
Dim gDBA As ADODB.Connection
Dim records As ADODB.Recordset
Dim datver As ADODB.Recordset
Dim query As String
'---OPEN THE DATABASE CONNECTIONS
gDBA = New ADODB.Connection ': gDBA.CursorLocation = adUseServer
'Added to prevent time out error
gDBA.CommandTimeout = 1000 : gDBA.ConnectionTimeout = 1000
gDBA.ConnectionString = "Server=svr13;Database=subscribers;User ID=KViews;Password=Solution;Trusted_Connection=True;"
gDBA.Open("Data Source=Kaseya;Initial Catalog=subscribers;User Id=KViews;Password=Solution;", "KViews", "Solution")
records = New ADODB.Recordset
query = "select distinct groupname from _v_k order by groupname desc"
'records.ActiveConnection = gDBA.ConnectionString
records.CursorType = CursorTypeEnum.adOpenForwardOnly
records.LockType = LockTypeEnum.adLockReadOnly
records.Open(query, gDBA)
Do While Not records.EOF
ComboBox1.Items.Add(records.Fields("groupname").Value)
records.MoveNext()
Loop
end Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selected As String
selected = ComboBox1.Text
Dim cryRpt As New ReportDocument
cryRpt.Load("C:\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\CrystalReport1.rpt")
cryRpt.SetDatabaseLogon("KViews", "Solutions", "svr13", "subscribers")
cryRpt.SetParameterValue("companyname", selected)
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
I looked at this previous posting database login prompt with crystal reports
but this wasn't very helpful. I couldn't find where a CMC was to disable the prompt.
Any ideas? thanks in advance

figured it out. the following line was incorrect
cryRpt.SetDatabaseLogon("KViews", "Solutions", "svr13", "subscribers")
it should just be this:
cryRpt.SetDatabaseLogon("KViews", "Solutions")
the login prompt doesn't show up anymore.

Related

Reading to a file line by line multiple choice quiz vb.net

I'm trying to create a multi choice quiz that will read questions and answers from a text file and if the RadioButton selected by the user contains text the same as the answer then the mark will increment by 2. I have tried every single loops and techniques but it either reads the last line only (for loop) or doesn't read the text file at all (do until / do while / while). I need help.
This is what I have done so far
Imports System.IO
Public Class Form5
Dim easy As Boolean
Dim medium As Boolean
Dim difficult As Boolean
Dim easytest As New System.IO.StreamReader("he.txt")
Dim lineseasy() As String = IO.File.ReadAllLines("he.txt")
Dim mediumtest As New System.IO.StreamReader("hm.txt")
Dim linesmedium() As String = IO.File.ReadAllLines("hm.txt")
Dim difficulttest As New System.IO.StreamReader("hd.txt")
Dim fulldata As String
Dim mark As Integer = 0
Private Sub Form5_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Label2.Text = Form3.ComboBox1.Text
Label3.Text = Form3.ComboBox2.Text
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
easytest.Read()
Dim counter As Integer = 0
Dim answer As String
While (easytest.Peek() <> -1)
Dim items() As String = lineseasy(1).Split(",")
Label4.Text = items(0)
RadioButton1.Text = items(1)
RadioButton2.Text = items(2)
answer = Str(3)
End While
End Sub
End Class
Why do you want a textfile where if u use Access database or sql database, it will be very simple .
For example, you create a db in either sql or access, create a table in it, create 3 columns, 1st column for question id,2nd column for question and the last one for the answer.Now add your question id(any random alpha-numeric/any string value),questions and answers in specific cells one by one. Now in you vb.net app,add a label(make it invisible) that will contain the question id, add reference to System.Data.SqlClient(for sql database) or System.Data.OleDb(for access). Now use the following code to check if the answer is correct or not ..
'add this code to all 4 radio buttons
Private Sub RadioBtn1_CheckedChanged
'create your connection string
connectionstring.open
Dim cmd as new SqlCommand("Select * from [table name from database-remove brackets if required] where [question id]=#quid and [answer]=#ans",connectionstring) 'use OleDbCommand of access db
cmd.parametres.add("#quid",sqldbtype.varchar).value=label1.text 'use oledbtype for access db
cmd.parametres.add("#ans",sqldbtype.varcha).value=radiobutton1.text 'when adding this code to radio button2, chage radiobutton1.text with radiobutton2.text , when using it in radio button 3 do the same
Dim adapter As New SqlDataAdapter(cmd) ' use oledbdataadapter for access
Dim table As New DataTable
adapter.Fill(table)
If table.Rows.Count() <= 0 Then
msgbox("WRONG ANSWER")
Else
marktext.text = mark.text+2 ' to increase point by 2
'what ever you want to do when the answer is correct
end if
Now, i hope u know how to load the question/question id(i mean the data) from the database...if you don't that please leave a reply and i'll add them

Select commannd in Visual Basic not working properly

I am new to Visual Basic for Applications. I have a login system in my current program. You will understand the system by reading below. I have an Access database whose table looks like following :-
Name : Cookies
----------------------------
ID | Nme | Val |
----------------------------
The table is cleared and all the contents of it is deleted when the form is closed. Now, when a user signs in, a row is added :-
----------------------------
(id) | "user" | username |
----------------------------
Now, the user enters his id and password in the form index.vb and the command to add the row in the access database is also there in the index.vb file. After the row is added, the index.vb file is hidden and the file userpage.vb is shown. Now, I have seen this that when the user logs in, the row is added properly in the access file (by refreshing the access file manually) and everything is deleted properly in the table when the window is closed. So, there is no problem in inserting and deletion of rows. So, obviously, the problem is with the displaying part. My code for displaying the username is as follows (the code is in the userpage.vb file) :-
Dim Username As String = ""
Dim Conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Software\db.accdb")
Conn.Open()
Dim Cmd As New OleDb.OleDbCommand("Select Val From Cookies Where Nme='user'", Conn)
Dim Reader = Cmd.ExecuteReader
Do While Reader.Read
Username = Reader.Item("Val")
Loop
Label1.Text = "Welcome " & Username
Conn.Close()
The output that is given is "Welcome" and Username is "" even when the row has been added. Now, I have also seen this by experimenting that when I manually add a row in the access database with the same details that is going to be added in the database problematically, the program works fine. Any help, of course, will be appreciated and thanks for reading this long post.
UPDATE 1 ( as suggested by #Dimple)
My code for insertion is as follows(This code is in the index.vb page) :-
Dim usernameinput As String = TextBox1.Text
Dim Cmmd As New OleDb.OleDbCommand()
Cmmd.Connection = Conn
Cmmd.CommandText = "INSERT INTO Cookies (Nme, Val) Values('user','" & usernameinput & "')"
Cmmd.ExecuteNonQuery()
Dim Userpage As New User_page
Me.Hide()
Userpage.Show()
You said that Access displays row after manually refreshing Access file (database). Sometimes you need to Requery() the database after insertion. I had this problem while working with Access.
Form1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\db.accdb")
Conn.Open()
Dim usernameinput As String = "TextBox3.Text"
Dim Cmmd As New OleDb.OleDbCommand()
Cmmd.Connection = Conn
Cmmd.CommandText = "INSERT INTO Cookies (Nme, Val) Values('user','" & usernameinput & "')"
Cmmd.ExecuteNonQuery()
Conn.Close()
Me.Hide()
Form2.Show()
End Sub
End Class
Form2
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\db.accdb")
Conn.Open()
Dim usernameinput As String = "TextBox3.Text"
Dim Cmmd As New OleDb.OleDbCommand()
Cmmd.Connection = Conn
Cmmd.CommandText = "INSERT INTO Cookies (Nme, Val) Values('user','" & usernameinput & "')"
Cmmd.ExecuteNonQuery()
Conn.Close()
Me.Hide()
Form2.Show()
End Sub
End Class
I didnt even change the variables you named. Just avoided form designing by using msgbox.
My brother instead of using,
Do While Reader.Read
Username = Reader.Item("Val")
Loop
try
Do While Reader.Read = True
Username = Reader.Item(0)
Loop
Let me Know if it works for you.

Writing multiple SQL server tables as separate nodes to XML Document

I have multiple SQL server tables from which I want to pull data and write to XML. I want my XML to be formed like this:
<Data>
<Query1Table>
<Table>
<Column1>Data</Column1>
<Column2>Data</Column2>
...
</Table>
</Query1Table>
<Query2Table>
<Table>
<Column1>Data</Column1>
<Column2>Data</Column2>
...
</Table>
</Query2Table>
</Data>
I'm using datasets to write the xml, but the code I'm working with doesn't append the data, it overwrites:
Dim connetionString As String
Dim connection As SqlConnection
Dim adapter As SqlDataAdapter
Dim directory As String
Dim ds As New DataSet
Dim sql As String
connection = New SqlConnection(connetionString)
sql = "select * from scheduledata"
connection.Open()
adapter = New SqlDataAdapter(sql, connection)
adapter.Fill(ds)
ds.DataSetName = "Schedule"
ds.WriteXml(directory)
ds.Clear()
sql = "select * from costdata"
adapter = New SqlDataAdapter(sql, connection)
adapter.Fill(ds)
ds.WriteXml(directory)
I have tried adding it all to the same data set by calling the SQL queries at once, but that doesn't help to separate them in the XML -- it groups them in the same node.
I'm open to a different method if anyone has a good suggestion.
You've defined directory as a string, so ds.WriteXml will (create and) write to a file by that name.
Use a FileStream or XmlTextWriter instead.
The way that I write to .xml files (which works) is as follows
Private Sub AuthenticationContinuebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AuthenticationContinuebtn.Click
On Error GoTo PasswordHandler
GlobalVariables.Username = Usernametxtbx.Text
GlobalVariables.Password = Passwordtxtbx.Text
GetUsernamePassword()
Close()
Exit Sub
PasswordHandler:
MsgBox("Incorrect password or username.")
End Sub
Private Sub UserAuthenticationWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim FileName4 As String = "C:\Forte\UsernamePassword.xml"
Dim FileRead4 As XmlTextReader = New XmlTextReader("C:\Forte\UsernamePassword.xml")
'If statement to see if file exists.
If System.IO.File.Exists(FileName4) = True Then
Do While (FileRead4.Read)
Select Case FileRead4.NodeType
Case XmlNodeType.Text, XmlNodeType.Element
If FileRead4.Name = "Username" Then
FileRead4.Read()
Usernametxtbx.Text = FileRead4.Value
GlobalVariables.Username = Usernametxtbx.Text
End If
If FileRead4.Name = "Password" Then
FileRead4.Read()
Passwordtxtbx.Text = FileRead4.Value
GlobalVariables.Password = Passwordtxtbx.Text
End If
End Select
Loop
Else
MainBox.MainTextBox.AppendText(FileName4 & " could not be found. Settings are restored to defaults.")
MainBox.Logging(Date.Now & FileName4 & " could not be found. Settings are restored to defaults.")
End If
FileRead4.Close()
End Sub
Public Sub createNode3(ByVal Username As String, ByVal Password As String, ByVal writer As XmlTextWriter)
'On Error Resume Next
writer.WriteStartElement("Username_Password")
writer.WriteStartElement("Username")
writer.WriteString(Username)
writer.WriteEndElement()
writer.WriteStartElement("Password")
writer.WriteString(Password)
writer.WriteEndElement()
writer.WriteEndElement()
End Sub

Crystal Report always asks for database login

I need your help.
I'm writing code for a shop program and I am using vb.net 2008 with Crystal Report version 10.5.37xxxx
The problem is when I'm trying to install my program on the client computer, everything works but not in my Crystal Report. It always asks for the database login and I did not code my program to ask for the database login.
I just wrote it in simple code like this:
Public Class Form16
Private Sub Form16_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim view As New CrystalReport4
view.SetParameterValue("p_1", Form5.no_faktur_tb_immanuel)
CrystalReportViewer1.ReportSource = view
End Sub
End Class
Can anyone help me with this?
You should be able to manually code the login credentials.
Public Class Form16
Private Sub Form16_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim view As New CrystalReport4
Dim user as string = "Username"
Dim pwd as string = "Password"
view.SetDatabaseLogon(user, pwd)
view.SetParameterValue("p_1", Form5.no_faktur_tb_immanuel)
CrystalReportViewer1.ReportSource = view
End Sub
End Class
Make Sure your Report DataSource Provider is set to Microsoft OLE DB provider for SQL Server and not SQL Server Native Client 10.0
try that open field explorer---> database field --->Right Click -->current Data source --->reports connection----->report ----->property ---->
set Property as---
Data Source: .\Databasename.accdb
and code on viewer form load as
Dim cryRpt As New ReportDocument
Dim Report1 As New rptItemWise
Dim strServerName As String
strServerName = Application.StartupPath
rptItemWise.SetDatabaseLogon("admin", "", strServerName, "dastabasename.accdb", True)
cryRpt.Load(Application.StartupPath + "\rptItemWise.rpt")
also change the report connection same as data source
i think that code work for you ....
This will work for you!
C# Code :
ConnectionInfo boconnectioninfo = new ConnectionInfo ();
boconnectioninfo.ServerName= "D-2818-w2k";
boconnectioninfo.DatabaseName ="NW";
boconnectioninfo.UserID ="sa";
boconnectioninfo.Password ="sa";
CrystalReportViewer1 .ReportSource = Server.MapPath("CrystalReport1.rpt");
TableLogOnInfos botableInfo= CrystalReportViewer1 .LogOnInfo;
foreach (TableLogOnInfo obj2 in botableInfo)
{
obj2.ConnectionInfo =boconnectioninfo;
}
Vb.net Code :
Dim boconnectioninfo As New ConnectionInfo
boconnectioninfo.ServerName = "sa"
boconnectioninfo.DatabaseName = "sa"
boconnectioninfo.UserID = "sa"
boconnectioninfo.Password = "sa"
Me.rptViewer.ReportSource = _ReportPath
Me.rptViewer.SelectionFormula = _SelectionFormula
If Not _ParameterFields Is Nothing Then
Me.rptViewer.ParameterFieldInfo = _ParameterFields
End If
Dim a As TableLogOnInfos = Me.rptViewer.LogOnInfo
' Iterate through the list.
For Each aa As TableLogOnInfo In a
aa.ConnectionInfo = boconnectioninfo
Next
Me.Cursor = Cursors.Default
rptViewer.Refresh()

Database Connection is Not Ending

I have the following code to insert some data from a GridView to a oracle database.
Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
Dim con = New OleDbConnection("Data Source=sml2; User ID=sfpl; Password=a; provider=OraOLEDB.Oracle")
con.Open()
For Each gvRow As GridViewRow In GridView1.Rows
Dim trndate As Date = ddlTrnDte.SelectedValue
Dim ZoneNo As Integer = ddlZone.SelectedValue
Dim DocNo As TextBox = CType(gvRow.FindControl("txtDocNo"), TextBox)
Dim VehCod As DropDownList = CType(gvRow.FindControl("ddlVeh"), DropDownList)
Dim CircleNo As DropDownList = CType(gvRow.FindControl("ddlCircle"), DropDownList)
Dim Contractor As DropDownList = CType(gvRow.FindControl("ddlContractor"), DropDownList)
Dim Supplier As DropDownList = CType(gvRow.FindControl("ddlSupplier"), DropDownList)
Dim NetWt As TextBox = CType(gvRow.FindControl("txtNetWt"), TextBox)
Dim Rate As TextBox = CType(gvRow.FindControl("txtRate"), TextBox)
Dim SNF As TextBox = CType(gvRow.FindControl("txtSNF"), TextBox)
Dim FAT As TextBox = CType(gvRow.FindControl("txtFAT"), TextBox)
Dim LR As TextBox = CType(gvRow.FindControl("txtLR"), TextBox)
Dim TS As TextBox = CType(gvRow.FindControl("txtTS"), TextBox)
'Create Connection
Dim cmd As OleDbCommand = New OleDbCommand("Insert into MLK_02_01 (TRN_DTE,ZONE_NO,CIRCLE_NO,CNT_NO,DOC_NO,SUP_COD,VEH_NUM,NET_WEIGHT,Q_SNF,Q_FAT,Q_LR,Q_TS,RATE,veh_cod) VALUES (:TRN_DTE,:ZONE_NO,:CIRCLE_NO,:CNT_NO,:DOC_NO,:SUP_COD,:VEH_NUM,:NET_WEIGHT,:Q_SNF,:Q_FAT,:Q_LR,:Q_TS,:RATE,:veh_cod)", con)
cmd.Parameters.Clear()
'TrnDate
cmd.Parameters.Add(":trn_dte", (OleDb.OleDbType.Date))
cmd.Parameters(":trn_dte").Value = DateTime.Now()
cmd.Parameters(":trn_dte").Value = ddlTrnDte.SelectedValue
cmd.Parameters.AddWithValue(":ZONE_no", ZoneNo)
cmd.Parameters.AddWithValue(":CIRCLE_no", CircleNo.SelectedValue)
cmd.Parameters.AddWithValue(":CNT_no", Contractor.SelectedValue)
cmd.Parameters.AddWithValue(":Doc_No", DocNo.Text)
cmd.Parameters.AddWithValue(":sup_cod", Contractor.SelectedValue)
cmd.Parameters.AddWithValue(":Veh_num", VehCod.SelectedItem.Text)
cmd.Parameters.AddWithValue(":NET_WEIGHT", NetWt.Text)
cmd.Parameters.AddWithValue(":Q_SNF", SNF.Text)
cmd.Parameters.AddWithValue(":Q_FAT", FAT.Text)
cmd.Parameters.AddWithValue(":Q_LR", LR.Text)
cmd.Parameters.AddWithValue(":Q_TS", TS.Text)
cmd.Parameters.AddWithValue(":rate", Rate.Text)
cmd.Parameters.AddWithValue(":veh_cod", VehCod.SelectedValue)
cmd.ExecuteNonQuery()
Label1.Text = "All Records are Saved Successfully"
con.Close()
btnInsert.Enabled = False
Next
End Sub
Data is inserted into database fine but when the form is closed the database connection does not end. I need help in ending the database session to eliminate further communication errors.
You mention it's a page, so I'm assuming this is an ASP page.
IIS has database connection pooling enabled by default. This means that even though your code closed your connection, IIS maintains a connection in order to make the next query from the next request execute faster.
To disable connection pooling, in your database connection string add this:
Pooling=false
New OleDbConnection("Data Source=sml2; User ID=sfpl; Password=a; provider=OraOLEDB.Oracle; Pooling=false")
Also, after your execute change to this:
cmd.ExecuteNonQuery()
cmd.Dispose()
After the con.Close():
con.Close()
con.Dispose()
and that should dispose the command and connection objects.