Private Sub Product_Details1(ByVal pid1 As String)

        Dim n As Integer
        n = 0
        DataGridView1.Rows.Clear()


        myConnection = New SqlConnection(connectionstring)
        myConnection.Open()
        Try
            sqlcmd = New SqlCommand("SELECT P_Id,P_Selling_Price FROM Product_Details WHERE P_Id='" & ComboBox1.SelectedText & "' ", myConnection)
            Dim dr As SqlDataReader = sqlcmd.ExecuteReader



            If dr.HasRows Then
                While dr.Read()

                    DataGridView1.Rows.Add()
                    DataGridView1.Rows(n).Cells(0).Value = dr("P_Id").ToString
                    DataGridView1.Rows(n).Cells(1).Value = dr("P_Selling_Price").ToString
                    DataGridView1.Rows(n).Cells(2).Value = TextBox1.Text
                    DataGridView1.Rows(n).Cells(3).Value = Val(DataGridView1.Rows(n).Cells(2).Value) * Val(DataGridView1.Rows(n).Cells(1).Value)
                    n += 1
                End While
            Else
                MessageBox.Show("Data doesn't exits  !!", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

            End If


            sqlcmd.Cancel()

            dr.Close()

            myConnection.Close()

        Catch ex As Exception

        End Try

    End Sub

the data is not takin from either from tables or textbox
the message is dsplay as Data doesn't exits
this is the coding to enter button to transfer the data from textbox and table to datagridview

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pid As String = ComboBox1.SelectedItem
        myConnection = New SqlConnection(Module1.connectionString)
        myConnection.Open()

        newq.Text = Val(TextBox2.Text) - Val(TextBox1.Text)

        Dim majortable As SqlCommand = New SqlCommand("Update Product_Details Set Quantity ='" & newq.Text & "' WHERE P_ID='" & ComboBox1.SelectedItem & " '", myConnection)

        majortable.ExecuteNonQuery()


        MessageBox.Show("Changes had done suceesfully. !", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''





        myConnection.Close()

        Product_Details1(pid)
        refreshComponents()

    End Sub

Recommended Answers

All 4 Replies

You should add

MsgBox(ex.Message)

in your catch block, for further error messages.

A sample code

'Variables
Dim myConnection As DbConnection
Dim factory As DbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient")
Dim command As DbCommand
Dim dr As DbDataReader
Dim adapter As DbDataAdapter = factory.CreateDataAdapter()
Dim row as integer = 0

'Initialize values
  myConnection = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection()
  myConnection.ConnectionString = "server=localhost;user id=sa;password=12345;database=Masterfile;"

'Connection open, Initialize command, then execute passing data to DR
  myConnection.Open()
  command = myConnection.CreateCommand()
  command.Connection = myConnection
  command.CommandText = query
  dr = command.ExecuteReader

 'Retrieve data
  While dr.read()
    DataGridView1.Item(column,row).Value = dr.GetString(dr.GetOrdinal("columnname")) 'retrieve string value of column
    row += 1
  End While

Hope this helps

The Problem is
it doesn't reach to this part

sqlcmd = New SqlCommand("SELECT P_Id,P_Selling_Price FROM Product_Details WHERE P_Id='" & ComboBox1.SelectedText & "' ", myConnection)
            Dim dr As SqlDataReader = sqlcmd.ExecuteReader

            While dr.Read()
                ' DataGridView1.Item(column, row).Value = dr.GetString(dr.GetOrdinal("columnname")) 'retrieve string value of column

                DGV1.Rows.Add()
                DGV1.Rows(n).Cells(0).Value = dr.GetString(dr.GetOrdinal("P_Id"))
                DGV1.Rows(n).Cells(1).Value = dr.GetString(dr.GetOrdinal("P_Selling_Price"))
                DGV1.Rows(n).Cells(2).Value = TextBox1.Text
                DGV1.Rows(n).Cells(3).Value = Val(DGV1.Rows(n).Cells(2).Value) * Val(DGV1.Rows(n).Cells(1).Value)
                n += 1
            End While

it directly reachs to else part.........

If dr.HasRows Then
                'ok
                MessageBox.Show("Succesfully updated  !!", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
            Else
                MessageBox.Show("Data doesn't exits  !!", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

            End If

I think'** where part** condition in the Sql statement is not working

There is nothing wrong with the syntax of the query.
Check the value of combobox1.selectedtext by
creating a temporary messagebox to show it's value
or try querying it in sql management studio.

thank you daniel1955

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.