Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I am new in programming and I hope all with respect to help me
I create a form with vb.net EX: "main Branches " and I want to print current data record so I create another form and put RDCL report in it
and it is run well but showing all record. i want the report show only the current record

What I have tried:

VB
'''''''''''''''''''''''''''''''''''''''
' the form number 1 (FRM_BR.vb)'
 Private Sub BTN_PRINT_Click(sender As Object, e As EventArgs) Handles BTN_PRINT.Click

        Dim f As New Form1
        f.ShowDialog()


       
    End Sub
'''''''''''''''''''''''''''''''''''''''''''''''
' the form number 2 (form1.vb)'
Private Sub ReportViewer1_Load(sender As Object, e As EventArgs) Handles ReportViewer1.Load

        Dim codee As String = FRM_BR.TXT_code.Text
        Dim CON As New SqlConnection("Data Source=MOHAMEDTHRWAT20\SQLEXPRESS;Initial Catalog=data_egy;User ID=sa")
        Dim COM As New SqlCommand("SELECT * FROM [dbo].[tb_br] where code ='" & codee & "' ", CON)
        Dim SD As New SqlDataAdapter(COM)
        Dim DTa As New DataTable

        SD.Fill(DTa)

        With Me.ReportViewer1.LocalReport
            .DataSources.Clear()
            .ReportPath = "C:\Users\mothr\onedrive\المستندات\visual studio 2012\Projects\WindowsApplication1\WindowsApplication1\PL\PL_ACC\Report1.rdlc"
            .DataSources.Add(New ReportDataSource("DataSet1", DTa))

        End With
        Me.ReportViewer1.RefreshReport()
    End Sub
Posted
Updated 29-Jul-22 6:13am
Comments
Mike Hankey 19-Jul-22 17:43pm    
I don't know VB very well but it looks like your SQL is correct.
Have you put a break to see what the value of 'codee' is in the SQL statement?
mohamed thrwat 19-Jul-22 19:02pm    
i put a value manually and it runs ok, I just want the value of the current record in the form 1
CHill60 20-Jul-22 3:22am    
Not a solution, but your code is open to SQL Injection attacks - never concatenate user input into strings to form a sql command see SQL Injection Attacks and Some Tips on How to Prevent Them[^]
Maciej Los 20-Jul-22 7:18am    
Does [code] field is type of string?
Maciej Los 22-Jul-22 3:30am    
Have you tried to use somethig like this: report.SetDataSource(ds)?!?

Seems you forgot to properly instantiate and fill in datatable object.

VB.NET
'skipped lines...
SD.Fill(DTa)
'below line produces empty datatable object, so...
'replace this line :
Dim DTa As New DataTable
'with:
Dim DTa As DataTable = SD.Tables(0)

With Me.ReportViewer1.LocalReport
    .ProcessingMode = ProcessingMode.Local
    .DataSources.Clear()
    .DataSources.Add(New ReportDataSource("DataSet1", DTa))
    'or without creation of datatable object
    '.DataSources.Add(New ReportDataSource("DataSet1", SD.Tables(0)))
    .LocalReport.Refresh()
    .RefreshReport()
End With


Note: Do not forget to change your code accordingly to @CHill recommedations!
 
Share this answer
 
v3
Comments
CHill60 22-Jul-22 9:15am    
Good spot! I stared at this for ages. One small point though - DTa is already declared and used on the line above. Might be worth clarifying that bit of code
Maciej Los 22-Jul-22 9:36am    
Thank you, Caroline.
I wasn't detailed enough. Hope my explanation is (almost) perfect this time :)
I solved by passing the value of the textbox in the form 1 to textbox in the form 2


VB.NET
 'the form number 1 (FRM_BR.vb)'

 Private Sub BTN_PRINT_Click(sender As Object, e As EventArgs) Handles BTN_PRINT.Click
        Try
            If TXT_Guid.Text = "" Then
                MessageBox.Show("لاتوجد بيانات للطباعة ")
            Else
                Dim obb As New Form1
                obb.val = Me.TXT_id.Text()
                obb.Show()
            End If
' the form number 2 (form1.vb)'

Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.Reporting.WinForms
Imports System.Data.SqlClient

Public Class Form1
    Public Property val As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.ReportViewer1.RefreshReport()
    End Sub
    Private Sub ReportViewer1_Load(sender As Object, e As EventArgs)

        TextBox1.Text = val
        Dim id As String = TextBox1.Text
        Dim CON As New SqlConnection("Data Source=MOHAMEDTHRWAT20\SQLEXPRESS;Initial Catalog=data_egy;User ID=sa")
        Dim COM As New SqlCommand("SELECT * FROM [dbo].[tb_br] where id ='" & id & "' ", CON)
        Dim SD As New SqlDataAdapter(COM)
        Dim DTa As New DataTable

        SD.Fill(DTa)

        With Me.ReportViewer1.LocalReport
            .DataSources.Clear()
            .ReportPath = "C:\Users\mothr\onedrive\المستندات\visual studio 2012\Projects\WindowsApplication1\WindowsApplication1\PL\PL_ACC\Report1.rdlc"
            .DataSources.Add(New ReportDataSource("DataSet1", DTa))

        End With
        Me.ReportViewer1.RefreshReport()
        
    End Sub
End Class


        Catch ex As Exception
        End Try
    End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900