Click here to Skip to main content
15,885,987 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this code but I think this is too long...there anything you can help me
VB
no_lab          char1s                  t1  t2  t3  t4  avrg    percn 
G0493/V/2015    Normal Seedling (%)     99  96  98  92  96.25   96.00
G0493/V/2015    Abnormal Seedling (%)   1   4   2   7   3.50    4.00
G0493/V/2015    Hard Seed (%)           0   0   0   0   0.00    0.00
G0493/V/2015    Fresh Seed (%)          0   0   0   0   0.00    0.00
G0493/V/2015    Dead Seed (%)           0   0   0   1   0.25    0.00
G0493/V/2015    Index Vigor (%)         87  83  84  81  83.75   84.00
G0493/V/2015    Germination (%)         99  96  98  92  96.25   96.00


VB
openDB()
sql = "Select * From wip_vg WHERE (no_lab= '" & tnolab.Text & "') and (char1s='" & Label62.Text & "')"
cmd = New SqlClient.SqlCommand(sql, con)
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
dread = cmd.ExecuteReader
While dread.Read
    tVGns1.Text = dread.Item("t1")
    tVGns2.Text = dread.Item("t2")
    tVGns3.Text = dread.Item("t3")
    tVGns4.Text = dread.Item("t4")
    tVGavrgNs.Text = dread.Item("avrg")
    tVGpcnNs.Text = dread.Item("percn")
End While
con.Close()

openDB()
sql = "Select * From wip_vg WHERE (no_lab= '" & tnolab.Text & "') and (char1s='" & Label63.Text & "')"
cmd = New SqlClient.SqlCommand(sql, con)

cmd.CommandType = CommandType.Text
cmd.CommandText = sql
dread = cmd.ExecuteReader
While dread.Read
    tVGas1.Text = dread.Item("t1")
    tVGas2.Text = dread.Item("t2")
    tVGas3.Text = dread.Item("t3")
    tVGas4.Text = dread.Item("t4")
    tVGavrgAs.Text = dread.Item("avrg")
    tVGpcnAs.Text = dread.Item("percn")
End While
con.Close()

openDB()
sql = "Select * From wip_vg WHERE (no_lab= '" & tnolab.Text & "') and (char1s='" & Label65.Text & "')"
cmd = New SqlClient.SqlCommand(sql, con)
'openDB()
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
dread = cmd.ExecuteReader
While dread.Read
    tVGhs1.Text = dread.Item("t1")
    tVGhs2.Text = dread.Item("t2")
    tVGhs3.Text = dread.Item("t3")
    tVGhs4.Text = dread.Item("t4")
    tVGavrgHs.Text = dread.Item("avrg")
    tVGpcnHs.Text = dread.Item("percn")
End While
con.Close()


openDB()
sql = "Select * From wip_vg WHERE (no_lab= '" & tnolab.Text & "') and (char1s='" & Label66.Text & "')"
cmd = New SqlClient.SqlCommand(sql, con)
'openDB()
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
dread = cmd.ExecuteReader
While dread.Read
    tVGfs1.Text = dread.Item("t1")
    tVGfs2.Text = dread.Item("t2")
    tVGfs3.Text = dread.Item("t3")
    tVGfs4.Text = dread.Item("t4")
    tVGavrgFs.Text = dread.Item("avrg")
    tVGpcnFs.Text = dread.Item("percn")
End While
con.Close()

openDB()
sql = "Select * From wip_vg WHERE (no_lab= '" & tnolab.Text & "') and (char1s='" & Label67.Text & "')"
cmd = New SqlClient.SqlCommand(sql, con)
'openDB()
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
dread = cmd.ExecuteReader
While dread.Read
    tVGds1.Text = dread.Item("t1")
    tVGds2.Text = dread.Item("t2")
    tVGds3.Text = dread.Item("t3")
    tVGds4.Text = dread.Item("t4")
    tVGavrgDs.Text = dread.Item("avrg")
    tVGpcnDs.Text = dread.Item("percn")
End While
con.Close()


openDB()
sql = "Select * From wip_vg WHERE (no_lab= '" & tnolab.Text & "') and (char1s='" & Label64.Text & "')"
cmd = New SqlClient.SqlCommand(sql, con)
'openDB()
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
dread = cmd.ExecuteReader
While dread.Read
    tVGiv1.Text = dread.Item("t1")
    tVGiv2.Text = dread.Item("t2")
    tVGiv3.Text = dread.Item("t3")
    tVGiv4.Text = dread.Item("t4")
    tVGavrgIv.Text = dread.Item("avrg")
    tVGpcnIv.Text = dread.Item("percn")
End While
con.Close()


openDB()
sql = "Select * From wip_vg WHERE (no_lab= '" & tnolab.Text & "') and (char1s='" & Label68.Text & "')"
cmd = New SqlClient.SqlCommand(sql, con)
'openDB()
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
dread = cmd.ExecuteReader
While dread.Read
    tVGgm1.Text = dread.Item("t1")
    tVGgm2.Text = dread.Item("t2")
    tVGgm3.Text = dread.Item("t3")
    tVGgm4.Text = dread.Item("t4")
    tVGavrgGm.Text = dread.Item("avrg")
    tVGpcnGm.Text = dread.Item("percn")
End While
con.Close()
Posted
Comments

The length does not matter so much, but your code could be refactored to get rid of these flaws:

- There is no need to close then reopen the connection between each request.

- Never, ever, construct SQL queries by concatenating string obtained from user inputs. This leaves your code opened to SQL injection attacks. Better use parameterized queries instead.

For example:
C#
sql = "Select * From wip_vg WHERE (no_lab = @noLab) and (char1s = @char1s)";
// ...
cmd.Parameters.AddWithValue("@noLab", tnolab.Text);
cmd.Parameters.AddWithValue("@char1s", Label65.Text);
// ...
 
Share this answer
 
Never mind the length of your code: that is the least of your problems!

Were you aware that your users can damage or delete your database just by typing in the text boxes? Do not 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.

Then look at why you are looping on yours SqlReaders, and only using the final result; and why you have several sets of almost identical code...
 
Share this answer
 
Even code 10000 times bigger will not be a problem for the compiler, but it will be a problem for you.
So, for the compiler, there is no practical limit that you will reach any time soon.
the only practical limit is you because soon you will be unable to maintain that huge code, and organising in smaller pieces of code (many files) will help you.

The only concern with your code is the inefficiency. because inefficient code is slow.
see other solutions for a list of what should not be done.
 
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