hi there.
I have a problem in making my amortization.
I just only want to know how to change data grid view's command text with where conditions.

this what i've got:

LoanSampleDBDataSetTableAdapters.loan1TableAdapter.commandText = "Select * from Employees where ID =" & n1

Then error occurs.

Dani AI

Generated

Summary and focused fixes for 's thread (attempting to filter the grid by ID). The runtime error most often comes from one of these: trying to mutate a typed TableAdapter the wrong way, a missing/invalid connection, uninitialized values (e.g. curDate in the posted code), or building SQL with unsafe concatenation. correctly pushed for the error text and showed adapter-command structure; rightly flagged the connection-string issue. The safest paths are to (A) let the typed TableAdapter do parameterized queries, (B) filter the in-memory view, or (C) run a short, parameterized query and bind the resulting DataTable.

Recommended approaches

  1. Add a parameterized query to the typed TableAdapter (DataSet Designer -> Add Query -> SQL with @ID). Visual Studio will generate a FillBy/GetDataBy method. Call it like:
' call the generated method (name will depend on what was created in the designer)
Me.Loan1TableAdapter.FillByID(Me.LoanSampleDBDataSet.loan1, CInt(TextBox1.Text))
  1. Filter the already-loaded table client-side (no new DB call):
Dim bs As BindingSource = TryCast(DataGridView1.DataSource, BindingSource)
If bs IsNot Nothing Then
    bs.Filter = "ID = " & CInt(TextBox1.Text)
End If

Troubleshooting checklist (concrete, verifiable fixes)

  • Verify getconn() actually returns a valid connection string and that con.Open() succeeds. Log/inspect exception text in a Try/Catch.
  • Initialize curDate before inserting (eg. curDate = DateTime.Now) or use a DateTimePicker value.
  • Stop building SQL with raw concatenation for inserts/selects; use parameters to avoid type/format errors and SQL injection.
  • Wrap connections/commands in Using blocks and dispose objects. For many inserts, consider a transaction or a bulk-load approach.

These steps address the core gaps in the posted code and follow the safer, designer-driven and parameterized patterns hinted at by the other replies.

Recommended Answers

All 6 Replies

Member Avatar for Member #949455

Then error occurs.

It's nice that you post that single line code but it's more important to post the errors when you ran your whole code. Without understanding the error, it will be very hard to resolve the issue you are having.

This doesn't look right:

"Select * from Employees where ID =" & n1

It should look like this:

"SELECT FROM Employees WHERE ID = " + ID;
Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class Form1
    Dim cmd As SqlCommand
    Dim con As SqlConnection
    Dim str As String
    Dim n1, n2 As Integer
    Dim d1, d2, d3 As Decimal
    Dim t1, t2 As Decimal
    Dim p1 As Decimal
    Dim curDate As Date
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        n1 = Convert.ToInt64(TextBox1.Text)
        d1 = Decimal.Parse(TextBox2.Text) 'loan amount
        d2 = Decimal.Parse(TextBox3.Text) 'interest rate
        n2 = Integer.Parse(TextBox4.Text)
        d3 = d1 / 100
        t1 = d1 * d3 'intereset
        p1 = d1 / n2 'monthly principal
        t2 = p1 + t1
        TextBox5.Text = t2
        con = getconn()
        con.Open()
        Dim loop1 As Integer
        For loop1 = 1 To n2

            str = "insert into loan1(ID,PrincipalAMount,interest,Date) VALUES(" & n1 & "," & p1 & "," & t1 & ",'" & curDate & "')"
            cmd = New SqlCommand(str, con)
            cmd.ExecuteNonQuery()

        Next
        cmd.Dispose()
        con.Close()
        Call Me.Loan1TableAdapter.Fill(Me.LoanSampleDBDataSet.loan1)
        'Me.Loan1TableAdapter.Fill(Me.LoanSampleDBDataSet.loan1, Me.TextBox1.Text)
    End Sub



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Me.Loan1TableAdapter.Fill(Me.LoanSampleDBDataSet1.loan1)


        'LoanSampleDBDataSetTableAdapters.loan1TableAdapter.commandText = "Select * from Employees where ID =" & n1

        ' Me.EmployeesTableAdapter.FillByFirstName(Me.EmployeesDataSet.Employees, Me.txtSearch.Text)
        'Dim da As New LoanSampleDBDataSetTableAdapters.loan1TableAdapter
        Me.Loan1TableAdapter.Fill(Me.LoanSampleDBDataSet.loan1)
        'Dim cmd As New SqlCommand
        ''Dim conn As New SqlConnection("Connection string here")
        'Dim MySQLAdapter As New SqlDataAdapter
        'Dim MyTable As New DataTable

        'With MySQLAdapter
        '    ' .SelectCommand.Connection = conn
        '    .SelectCommand.CommandText = "SELECT * FROM loan1 WHERE ID=" & TextBox1.Text
        '    .Fill(MyTable)
        '    .Dispose()
        'End With
        'DataGridView1.DataSource = MyTable

I'm stack here!!! 
    End Sub


End Class

n1 is associated with the textbox1.text
I just want to display the datagrid view where ID=n1

image24

what code should I insert after "Where ID="

Member Avatar for Member #949455

I don't have a query builder. Try this:

DeleteCommand=
    "DELETE FROM [loan1] WHERE [ID] = @ID"

InsertCommand=
    "INSERT INTO [loan1] ([ID], [PrincipalAMount], [interest], [Date])
     VALUES (@n1, @p1, @t1, @curDate)"

SelectCommand=
    "SELECT [ID], [PrincipalAMount], [interest], [Date]
     FROM [loan1]"

UpdateCommand=
    "UPDATE [loan1] SET [ID] = @n1,
     [PrincipalAMount] = @p1, [interest] = @t1, [Date] = @curDate
     WHERE [ID] = @ID"

Can you please post the error you are getting?
From your code I see that you have declared a connection, but didn't give it a connection sting.

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.