title: Sql server distinct keyword in vb.net
body: <p>Hi all i need a help in getting data from sql database to datagrid.
  I want to count distinct values and put it into the datagrid column which is also a distinct value like this:-</p>
  
  <blockquote>
  <pre><code>| Partyname | Turbo | Truck | Total |
  +-----------+-------+-------+-------+
  | Abc       | 33    | 3     | 36    |
  | xyz       | 30    | 10    | 40    |
  | qwe       | 2     | 4     | 6     |
  </code></pre>
  </blockquote>
  
  <p>But what i'm getting is this!:-</p>
  
  <blockquote>
  <pre><code>| Partyname | Turbo | Truck | Total |
  +-----------+-------+-------+-------+
  | Abc       |       |       |       |
  | xyz       |       |       |       |
  | qwe       |       |       |       |
  </code></pre>
  </blockquote>
  
  <p>And my code is:-</p>
  
  <pre><code>  Dim sCommand, cmd As SQLCommand
      Dim sadp As SQLDataAdapter
      Dim sds As New DataSet
      Dim stable As New DataTable
      Private Sub OK_Button_Click_1(sender As System.Object, e As System.EventArgs) Handles OK_Button.Click
          Dim sts As String = ""
          Dim dt, dt2 As String
          dt = (Date_from.Value.Date).ToShortDateString + " " + (DateTimeInput1.Text)
          dt2 = (Date_to.Value.Date).ToShortDateString + " " + (DateTimeInput2.Text)
          Try
              Credit_report_daily.Show()
              Dim sql As String
              sql = "SELECT Distinct Party_name FROM Weightment Where Cash_Cr='Credit' AND (Weigh_date Between @dt AND @dt2) Group by Party_name"
              Using com As New SqlCommand(sql, Con)
                  Con.Open()
                  com.Parameters.AddWithValue("@dt", DateTime.Parse(dt))
                  com.Parameters.AddWithValue("@dt2", DateTime.Parse(dt2))
                  sadp = New SqlDataAdapter(com)
                  sds = New DataSet()
                  sadp.Fill(sds, "General")
                  stable = sds.Tables("General")
                  Credit_report_daily.DataGridView1.DataSource = sds.Tables("General")
                  Dim cmd As New SqlCommand
                  cmd.Connection = Con
                  cmd.CommandText = "SELECT Distinct Vehicle_Type FROM Weightment Where Cash_Cr='Credit' AND (Weigh_date Between @dt AND @dt2) Group by Vehicle_Type"
                  cmd.Parameters.AddWithValue("@dt", DateTime.Parse(dt))
                  cmd.Parameters.AddWithValue("@dt2", DateTime.Parse(dt2))
                  Dim da As SqlDataReader = cmd.ExecuteReader
                  While da.Read
                      Credit_report_daily.DataGridView1.Columns.Add(da.Item(0), da.Item(0))
                  End While
                  da.Close()
                  Con.Close()
              End Using
          Catch ex As Exception
              MsgBox(ex.ToString)
          End Try
          Me.Close()
      End Sub
  </code></pre>
  
  <p>Can you plz help me??????</p>
  
  <p>i'm Getting column by counting how many distinct values are their in Column Vehicle_type
  Like this:-</p>
  
  <pre><code>                    Dim cmd As New SqlCommand
                      cmd.Connection = Con
                      cmd.CommandText = "SELECT Distinct Vehicle_Type FROM Weightment Where Cash_Cr='Credit' AND (Weigh_date Between @dt AND @dt2) Group by Vehicle_Type"
                      cmd.Parameters.AddWithValue("@dt", DateTime.Parse(dt))
                      cmd.Parameters.AddWithValue("@dt2", DateTime.Parse(dt2))
                      Dim da As SqlDataReader = cmd.ExecuteReader
                      While da.Read
                          Credit_report_daily.DataGridView1.Columns.Add(da.Item(0), da.Item(0))
                      End While
                      da.Close()
                      Con.Close()
  </code></pre>