since i am new to crystal report i have search for 3 hours still couldn't find the right answer to my problem.
please check my code.
Dim rptsumrep As CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim sda As New MySqlDataAdapter
Dim bsource As New BindingSource
Dim dtincom As New DataTable
dtincom.Clear()
conn.Open()
Dim queryIncom As String = "select *from tblbilling where date_conduct between '" & dtfrom.Value.Date.ToString("yyyy-MM-dd") & "' and '" & dtto.Value.Date.ToString("yyyy-MM-dd") & "'"
sda = New MySqlDataAdapter(queryIncom, conn)
sda.Fill(dtincom)
bsource.DataSource = dtincom
sda.Update(dtincom)
grid.DataSource = bsource
rptsumrep = New CrystalReport1
rptsumrep.SetDataSource(dtincom)
frmCrystalReport.CrystalReportViewer1.ReportSource = rptsumrep
frmCrystalReport.CrystalReportViewer1.Refresh()
frmCrystalReport.ShowDialog()
frmCrystalReport.Dispose()
Questions:
do i have to name my rpt field same to my datagridview column title?
why is it the last column title same as my database which i rename it as "Status" but on the run time it shows "status"
The column titles in Database and Dataset has to be the same.
Also, using Select Column1, Column2, ColumnN ... is always the better way than writing just Select * in query.
Titles should/can be edited in Report file, which will change the showing title of that column, of course the name of the column should stay as a original in Details section of report.
Related
Dim cmd As New SqlCommand("SELECT MainDepartmentsTable.ID, FeesTable.ID,FeesTable.Particular,FeesTable.Fee from MainDepartmentsTable inner join SubDepartmentsTable on MainDepartmentsTable.ID = SubDepartmentsTable.MainDeptID inner join FeesTable on SubDepartmentsTable.MainDeptID = FeesTable.SubDepartmentID WHERE FeesTable.ID = '" & TextDepartmentID.Text & "' ", con)
Dim DataAdopter = New SqlDataAdapter(cmd)
Dim dt As New DataTable
DataAdopter.Fill(dt)
Load_Deparments()
First you need to validate the user input.
Using blocks ensure that you connection and command are closed and disposed.
Parameters save your database from sql injection. I had to guess at the datatype.
Private ConStr As String = "Your connection string"
Private Sub OPCode()
Dim DeptID As Integer
If Not Integer.TryParse(TextDepartmentID.Text, DeptID) Then
MessageBox.Show("Please enter a valid department ID.")
Exit Sub
End If
Dim dt As New DataTable
Using con As New SqlConnection(ConStr),
cmd As New SqlCommand("SELECT MainDepartmentsTable.ID,
FeesTable.ID,
FeesTable.Particular,
FeesTable.Fee
from MainDepartmentsTable
inner join SubDepartmentsTable on
MainDepartmentsTable.ID = SubDepartmentsTable.MainDeptID
inner join FeesTable on
SubDepartmentsTable.MainDeptID = FeesTable.SubDepartmentID
WHERE FeesTable.ID = #DepartmentID;", con)
cmd.Parameters.Add("#DepartmentID", SqlDbType.Int).Value = DeptID
con.Open()
dt.Load(cmd.ExecuteReader)
End Using
Load_Departments(dt)
End Sub
Private Sub Load_Departments(dt As DataTable)
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Particular"
ComboBox1.ValueMember = "Fee"
End Sub
Let's simplify a bit:
Dim sql = "SELECT FeesTable.ID, CoNCAT(FeesTable.Particular, '-', FeesTable.Fee) as DisplayText
FROM
SubDepartmentsTable
inner join FeesTable on SubDepartmentsTable.MainDeptID = FeesTable.SubDepartmentID
WHERE SubDepartmentsTable.MainDeptID = #id"
Dim da As New SqlDataAdapter(sql, con)
da.SelectCommand.Parameters.AddWithValue("#id", Convert.ToInt32(TextDepartmentID.Text)) 'less worried about AWV with ints but read https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
Dim dt As New DataTable
da.Fill(dt)
'this part connects the data to the combo - it's the part you didn't show in your Q and what people are telling you is missing
yourCombo.DisplayMember = "DisplayText"
yourCombo.ValueMember = "ID"
yourCombo.DataSource = dt
This isn't intended to be a paste in fix for your problem; you've left out too much info from your question to know how to answer it exactly, so you're gonna have to modify this
Things you need to change:
The columns that are selected in the SQL - load one column you will show (I demonstrated how to combine two bits of info under one name of DisplayText) and one column for the backing value (ID)
The column that is filtered on in the where clause or the name of the text box TextDepartmentID - it seems odd to me that a Fee ID should be equal to a Department ID, so I've altered the SQL to retrieve the Fee details for a MainDepartmentID because I've assumed your textbox name is correct and that you intended to show the fees for a department via the subdepartments
Read the blog linked and perhaps choose adding a parameter in a different way
Change yourCombo to the name of your combobox
Change DisplayMember and ValueMember setting to match the names of the two columns your SQL query selects
You only need two columns if youre plumbing up a combo, but if you pass the datatable off somewhere you can add more
i using this code to search by textbox to get the related data for the ID
Dim connetionString As String
Dim cnn As SqlConnection
connetionString="DataSource=IP;InitialCatalog=DB;UserID=sa;Password=password"
cnn = New SqlConnection(connetionString)
Dim com As String = "SELECT * FROM Table WHERE ID= '" & TextBox1.Text & "'"
Dim Adpt As New SqlDataAdapter(com, cnn)
Dim ds As New DataSet()
Adpt.Fill(ds, "Table")
DataGridView1.DataSource = ds.Tables(0)
What iam trying to do, i need to do the same code but with datagridview column1
Explaining:
i've already add button to upload data to datagridview the data include ID's
i need to search by those ID's in bulk, that included to the datagridview then post the income data to another datagridview,
Thanks in advance.
First, I think you should correct your existing code to use SQL Parameters.
Dim com As New SqlCommand("SELECT * FROM Table WHERE ID= #Id;", cnn)
com.Parameters.Add("#Id", SqlDbType.VarChar).Value = TextBox1.Text
Dim Adpt As New SqlDataAdapter(com)
Now to use the value in the first row, first column
Dim strId As String = DataGridView1.Rows(0).Cells(0).Value.ToString
Dim com2 As New SqlCommand("SELECT * FROM Table WHERE ID= #Id;", cnn)
com2.Parameters.Add("#Id", SqlDbType.VarChar).Value = strId
Dim Adpt2 As New SqlDataAdapter(com2)
I had to guess at the datatype. Please check your database to get the correct type. I guessed it was some type of string since you had it enclosed in single quotes.
i am having table named as product in msaccess database, i was added crystal report into my project but it shows all records from table, i want to filter it between two dates using ms access query but i don't know how to do this... the source code is
Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim cmd1 As OleDbCommand = New OleDbCommand("SELECT distinct invoice,productname,amount from product where date between '" & date1.Text & "' and '" & date2.Text & "'", con)
myDA = New OleDbDataAdapter(cmd1)
myDataSet = New DataSet()
myDA.Fill(myDataSet, "outwardp")
saledtereport.CrystalReportViewer1.ReportSource = myDataSet
saledtereport.ShowDialog()
i am new to crystal reports please help me out of this problem developers..
thanks in advance.
You cannot set the dataset as report source
the report source should be the report that you have created and that you want to show in that report viewer
and then only you can add dataset to the report
Dim rpt As New pos_rpt_finalreportcr 'name of the crystal report that you have created
rpt.Load(Server.MapPath("pos_rpt_finalreportcr.rpt"))
CrystalReportViewer1.ReportSource = rpt
rpt.SetDataSource(myDS)
rpt.Refresh()
hope this helps you.
I am trying to add a new order into my database using this code. I have used the same code before however its not working now. Can you please help?
I am using Microsoft Visual Basic 2008 express edition and Microsoft Access.
I have four tables.
CurrentRowNo = 0
' Purpose: Add new Account record and assign its default values
' Clear the Account table in the DataSet ready for a new record to be added
SandRDataSet.Tables("Stock").Clear()
'Add a new record to the studnet table
Dim driveletter As String = Application.StartupPath.Substring(0, 1)
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & driveletter & ":\raheem\Computing\Database\SandR.accdb")
cn.Open()
Dim daStock As New OleDbDataAdapter("SELECT * FROM Stock;", cn)
Dim sqlcmdbldStudent As New OleDb.OleDbCommandBuilder(daStock)
'Fill the database
daStock.Fill(SandRDataSet, "Stock")
Dim test As DataRow = SandRDataSet.Tables("Stock").NewRow
test("Product/Service number") = txtProductNo.Text
test("Product/ Service name") = txtProductName.Text
test("minimum level") = txtMin.Text
test("maximum level") = txtMax.Text
test("Quantity") = txtQuantity.Text
test("Price") = txtPrice.Text
SandRDataSet.Tables("Stock").Rows.Add(test)
sqlcmdbldStudent.GetInsertCommand()
daStock.InsertCommand = sqlcmdbldStudent.GetInsertCommand
daStock.Update(SandRDataSet.Stock)
DisplayAccount()
cn.Dispose()
cn.Close()
If you really have that column names in your table (Space and forward slashes) then I suggest you to set the QuotePrefix and QuoteSuffix to your OleDbCommandBuilder
Dim sqlcmdbldStudent As New OleDb.OleDbCommandBuilder(daStock)
sqlcmdbldStudent.QuotePrefix = "["
sqlcmdbldStudent.QuoteSuffix = "]"
This will force the fields names created by the command builder to be enclosed in square brackets and correctly interpreted by the database engine
How do you use SQL to put data into an empty table? So far in my code I have taken the data from an SQL table and then have put it into a Text Box using the following code:
Dim query As String = "SELECT Pronunciation, Character FROM [Katakana List] WHERE Pronunciation='" & pronunciation & "';"
Dim instruction = New SqlCommand(query, sqlCon)
Dim da As New SqlDataAdapter
da.SelectCommand = instruction
da.Fill(Katakana)
Textbox1.text=DT.row(0)("column")
Now that the data is held in the text box, how would I do the reverse of this process to put the data into an empty table. It would help if someone could give me an example of the query I could use to put the data back in.
If you want update data which you get from query before, then use UPDATE-query
If you want insert brand new data then use INSERT - query
'For updating existing data
Dim query As String = "UPDATE [Katakana List] SET Character = #NewCharacter WHERE Pronunciation=#Pronunciation"
'For Inserting new data
Dim query As String = "INSERT INTO [Katakana List] (Character) VALUES (#NewCharacter);"
Using instruction As New SqlCommand(query, sqlCon)
'Better practice to use parameters in query
instruction.Parameters.AddWithValue("#Pronunciation", pronunciation)
instruction.Parameters.AddWithValue("#NewCharacter", Textbox1.text)
instruction.ExecuteNonQuery()
End Using
If you want to set the data in the textbox into datatable just do it like this :
DT.row(0)("column") = Textbox1.text
if you want to enter it into database create query to do so , something like this :
Dim query As String = "Insert Into TableName(col1) Values('"& Textbox1.text &"');"
Dim command = New SqlCommand(query, sqlCon)
command.ExecuteNonQuery()