Rita Shroff
Rita Shroff

Reputation: 175

Invalid Column Name issue


I am working in vb.net and facing an Issue of invalid column name when accessing the data from SQL.
I am using MSSQL as back-end.

I referred many links, googled alot but unable to solve my problem

The code where I am facing problem is :

Private Sub txtAccCode_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtAccCode.TextChanged
    Try
           mQry = " from FAMPAR where Account_Code = '" & txtAccCode.Text & "'"
        If Not CheckEof("select *" & mQry, con1) Then
            txtAccName.Text = GetData("select Party_Name " & mQry, con1)
            txtProPart.Text = GetData("select Proprietor" & mQry, con1)
            txtRef.Text = GetData("select Reference_By" & mQry, con1)
            txtAdd1.Text = GetData("select Address_1" & mQry, con1)
            txtAdd2.Text = GetData("select Address_2" & mQry, con1)
            txtCity.Text = GetData("select City" & mQry, con1)
            txtPhno.Text = GetData("select Phone_No" & mQry, con1)
            txtEmail.Text = GetData("select Email" & mQry, con1)
            txtDrugLic.Text = GetData("select Drug_License" & mQry, con1)
            txtGst.Text = GetData("select GST_No" & mQry, con1)
            txtCst.Text = GetData("select CST_No" & mQry, con1)
            txtCustBank.Text = GetData("select Customer_Bank" & mQry, con1)
            txtAreaCode.Text = GetData("select Area_Code" & mQry, con1)
            txtCustGrpCode.Text = GetData("select Cust_GrpCode" & mQry, con1)
            txtDisc.Text = GetData("select Disc_Perc" & mQry, con1)
            txtOpeningBal.Text = GetData("select Open_Bal" & mQry, con1)
            txtCurrBal.Text = GetData("select Curr_Bal" & mQry, con1)
            txtCcd.Text = GetData("select CC_DD" & mQry, con1)
            txtStopDays.Text = GetData("select Stop_Days" & mQry, con1)
            txtBillOutstdg.Text = GetData("select Fix_Days" & mQry, con1)
            txtRemarks.Text = GetData("select Description" & mQry, con1)
            cmbRetTax.Text = GetData("select Retail_Taxable" & mQry, con1)
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

The GetData function I used in above code is

Function GetData(qry As String, Xcn As SqlConnection)
    Try
        da = New SqlDataAdapter(qry, Xcn)
        dt = New DataTable
        da.Fill(dt)
        GetData = IIf(Trim(dt.Rows(0).Item(0)) <> "", Trim(dt.Rows(0).Item(0)), "")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Function

Now the problem is it works correct till textAreaCode.text but after than from textCustGrpCode.text error starts saying "invalid Column name (name of column)". The error screen-shot is shown in the link below..

Error Image-Click here

The other thing is when I twist txtAreaCode.text line position with txtCustGrpCode.text line or any other below it, textAreaCode.text gives the error as "Invalid Column Name(column Name)" and the other line works properly.

Plz tell me where I did mistake...
Any help will be accepted..!

Upvotes: 0

Views: 1266

Answers (2)

Why don't you use data adapter and data table to it would be a lot easy to put selected column data to the text box.

        strQuery = " SELECT * FROM DIVISION ORDER BY DIVISIONCODE"

        Dim DataAdapeter As New SqlDataAdapter(strQuery, myConnection)

        Dim dsDivision As New DataSet
        myConnection.Open()
        DataAdapeter.Fill(dsDivision)

        DtDivision = dsDivision.Tables("DIVISION")

        myConnection.Close()


            DrDivision = DtDivision.Rows(i)
            textbox1.text = DrDivision("DIVISION").ToString                     
            textbox2.text = DrDivision("DIVISIONCODE").ToString

this code might help you understand how to use table adapter and data table.

Upvotes: 1

Nadir Riyani
Nadir Riyani

Reputation: 14

Add Description field [Description] as this is keywoard. check other field if any

Upvotes: 0

Related Questions