Click here to Skip to main content
15,905,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I am beginner in Crystal Reports and vb.net. I want to split my database columns and show that columns in report and also in datagridview. Please help me asap.

What I have tried:

I was find the code but i didn't.
Posted
Updated 4-Oct-17 1:58am
Comments
OriginalGriff 4-Oct-17 7:29am    
And?
What have you tried?
Where are you stuck?
What help do you need?
Mak Patel 4-Oct-17 7:39am    
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btn_ms.Click
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim dtp1 As DateTime
Dim dtp2 As DateTime
Dim ss As String

dtp1 = dtp_1.Value
dtp2 = dtp_2.Value

Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
'con.Open()
cmd.Connection = con
ss = "select * from order_master where (order_date between CONVERT(VARCHAR,'" & dtp1.ToString("dd/MM/yyyy") & "') and CONVERT(VARCHAR,'" & dtp2.ToString("dd/MM/yyyy") & "')) and ms = '" & cb_ms.Text & "'"
cmd.CommandText = ss
da.SelectCommand = cmd
da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
dr = cmd.ExecuteReader()
dr.Close()
Form1.Show()
End Sub


This is my code to display data in datagridview. but i want to split my column of datagridview.

1 solution

Never do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And your SQL indicates deeper flaws: You take a DateTime value, convert it to a string in your C# code, then use SQL to convert that string to ... a string, which you then try to use as a Date in a BETWEEN statement. That implies that your order_date column is a string, which means that your BETWEEN will fail, as it uses string comparisons which stop at the first different character in the two strings. So
'31-01-1900' is after '01-12-2017' because '3' is after '0' in the character set, and SQL will stop looking at that point.

Change your DB to use DATE, DATETIME, or DATETIME2 to store your dates, and pass the DateTime value directly via a parameterised query while you fix all the other concatenations in your application!

Then you can explain what "split my column" actually means ... but that will have to wait until you have a "safe" code base to work from...
 
Share this answer
 
Comments
Mak Patel 4-Oct-17 8:05am    
Thanks for the solution. but i was used short date format in my forms than how to query between dates?

Thanks in advance.
OriginalGriff 4-Oct-17 8:19am    
A DateTime doesn't have a format in SQL, VB, or C# - it's a number of ticks since a specific point in time and only gets any formatting (or indeed concept of days, months or years) when it is deliberately formated for a user. If you DB contains "formatted dates" then they are stored as strings and have all the problems I mentioned above.
Mak Patel 4-Oct-17 8:38am    
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btn_ms.Click
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim dtp1 As Date
Dim dtp2 As Date
Dim ss As String

dtp1 = dtp_1.Value
dtp2 = dtp_2.Value

Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
'con.Open()
cmd.Connection = con
ss = "select * from order_master where order_date between '" & dtp1 & "' and '" & dtp2 & "' and ms = '" & cb_ms.Text & "'"
cmd.CommandText = ss
da.SelectCommand = cmd
da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
dr = cmd.ExecuteReader()
dr.Close()
End Sub

Now how to split my columns.

i have columns p_name that contains multiple products in single cell seprated by comma.
i want to display that columns through split.

for ex.

in db

p_name

ABC,XYZ

i want to show in datagridview

p_name
ABC
XYZ
OriginalGriff 4-Oct-17 10:28am    
And you are listening to nothing I say...

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