Update problem while editing text field + populate checkbox value from table

N

Nithin

My code as an txt attachment.

I have 2 drop down list boxes that on selection populate text boxes
from my database table. I am able to display the correct values in
these text boxes.
I have 2 questions:

1) I would like the user update these text fields that get populated
with data from the database. For some reason I get the error when I
change the textbox value.
Exception Details: System.Data.SqlClient.SqlException: Invalid column
name 'categoryid'.
Line 123: dbComm.ExecuteNonQuery()

My update query is:
Dim strSQL3 As String = "UPDATE Testmarket SET market = " &
txtMarket.Text & "WHERE categoryid='" & ddlCategory.SelectedValue() &
"'"

What I am doing wrong? categoryid is my primary key.


2) Also, I have checkboxes in the same page that have to be populated
if the column valus is 'Yes'? Do you have an idea of how to do that.

Code:
-------------------------------------------------------------------------------
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class dgrid3
Inherits System.Web.UI.Page

Private strSql As String
Private objConn As SqlConnection

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page her

If Not IsPostBack Then
'Fill Date DropDown
strSql = "SELECT categoryid, date FROM Testmarket ORDER BY
categoryid"
objConn = New
SqlConnection("server=(local)\NetSDK;database=Northwind;Integrated
Security=SSPI")
Dim objCmd As New SqlCommand(strSql, objConn)
Dim _reader As SqlDataReader

Try
objConn.Open()
_reader = objCmd.ExecuteReader
With ddlDate
.DataSource = _reader
.DataValueField = "categoryid"
.DataTextField = "date"
.DataBind()
End With

ddlDate.Items.Insert(0, "-Select-")
Catch ex As SqlException
Response.Write(ex.ToString)
Finally
_reader.Close()
objConn.Dispose()
End Try
End If
End Sub

Protected Sub FillCategory(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ddlDate.SelectedIndexChanged

'Fill Category DropDown
strSql = "SELECT categoryid FROM Testmarket " & _
"WHERE categoryid='" & ddlDate.SelectedValue() & "'"
& _
" ORDER BY categoryid"

objConn = New
SqlConnection("server=(local)\NetSDK;database=Northwind;Integrated
Security=SSPI")
Dim objCmd As New SqlCommand(strSql, objConn)
Dim _reader As SqlDataReader
Try
objConn.Open()
_reader = objCmd.ExecuteReader
With ddlCategory
.DataSource = _reader
.DataValueField = "categoryid"
.DataTextField = "categoryid"
.DataBind()
End With
ddlCategory.Items.Insert(0, "-Select-")
Catch ex As SqlException
Response.Write(ex.ToString)
Finally
_reader.Close()
objConn.Dispose()
End Try
End Sub

Protected Sub Fillmarket(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ddlCategory.SelectedIndexChanged
'Clear Titles Textbox
txtMarket.Text = ""
'Fill Titles TextBox
Dim strSql2 As String = "SELECT market FROM Testmarket " & _
"WHERE categoryid='" & ddlCategory.SelectedValue() &
"'"
objConn = New
SqlConnection("server=(local)\NetSDK;database=Northwind;Integrated
Security=SSPI")
Dim objCmd As New SqlCommand(strSql2, objConn)
Dim _reader As SqlDataReader
Try
objConn.Open()
_reader = objCmd.ExecuteReader(CommandBehavior.SingleRow)
If _reader.Read Then
txtMarket.Text = _reader("market")
End If
Catch ex As SqlException
Response.Write(ex.ToString)
Finally
_reader.Close()
objConn.Dispose()
End Try
End Sub

Private Sub Update_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Update.Click
objConn = New
SqlConnection("server=(local)\netSDK;database=Northwind;Integrated
Security=SSPI")
Dim strSQL3 As String = "UPDATE Testmarket SET market = " &
txtMarket.Text & "WHERE categoryid='" & ddlCategory.SelectedValue() &
"'"

Dim dbComm As New SqlCommand(strSQL3, objConn)
objConn.Open()
dbComm.ExecuteNonQuery()
objConn.Close()
End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top