When to trying to display data to datagrid view using query i got an error
"a column named date already belongs to this datatable in net 2008"

  Dim dt As New DataTable
        Try

            myConnection = New SqlConnection(connectionstring)
            myConnection.Open()


            Dim cmd As New SqlClient.SqlCommand
            With cmd
                .CommandText = "SELECT [Date],[Totalamount] FROM [Mobilization] where [Contract_Name]='" & ComboBox1.Text & "' AND [Client_Name]='" & ComboBox2.Text & "'"
                .CommandType = CommandType.Text

                .Connection = myConnection
            End With

            dt.Load(cmd.ExecuteReader)
            dt.Columns.Add(New DataColumn("Date", System.Type.GetType("System.String")))
            dt.Columns.Add(New DataColumn("Total Amount", System.Type.GetType("System.Double")))

            DataGridView1.DataSource = dt

            myConnection.Close()


        Catch err As Exception
            MsgBox(err.Message, MsgBoxStyle.Exclamation)
        End Try

I couldn't find what is the error

Recommended Answers

All 2 Replies

Don't add columns that are already been added by Load method. Remove line #17 to 19.

Thank you avd

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.