Hello Everyone & please excuse my ignorance. I am fairley new to .net and newer to SQL. The below code gives me an exception telling me that it is an invalid column name. It's taking the input string from "frmMain.txtBadge.Text" and thinking it's the column name. I have searched for days without any clue. Any help would be appreciated.
Thanks - Sub chkGunIn()
-
-
-
Dim daGun As New SqlDataAdapter
-
Dim dsGun As New System.Data.DataSet
-
Dim searchGunIn As String
-
searchGunIn = DateTime.Now
-
Dim mySelectQuery As String = ("UPDATE RF_Assign SET Gun_In = '" _
-
& searchGunIn & "' ,Batt_In = '" & searchGunIn _
-
& "' WHERE Badge = " & frmMain.txtBadge.Text)
-
-
Dim myConnection As New SqlConnection(connectionString)
-
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
-
Try
-
-
myConnection.Open()
-
daGun.SelectCommand = myCommand
-
daGun.Fill(dsGun)
-
myCommand.ExecuteNonQuery()
-
myConnection.Close()
-
cmdClearTXT()
-
myConnection.Close()
-
Catch ex As System.Exception
-
MessageBox.Show(ex.Message)
-
End Try
-
End Sub
12 3041 TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out. -
Sub chkGunIn(byVal strBadge as String)
-
Dim daGun As New SqlDataAdapter
-
Dim dsGun As New System.Data.DataSet
-
Dim searchGunIn As String = ""
-
Dim mySelectQuery As String = ""
-
-
searchGunIn = DateTime.Now
-
-
mySelectQuery = ""
-
mySelectQuery = ("UPDATE RF_Assign SET Gun_In = '" _
-
& searchGunIn & "' ,Batt_In = '" & searchGunIn _
-
& "' WHERE Badge = " & Trim(strBadge))
-
-
Dim myConnection As New SqlConnection(connectionString)
-
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
-
Try
-
-
myConnection.Open()
-
daGun.SelectCommand = myCommand
-
daGun.Fill(dsGun)
-
myCommand.ExecuteNonQuery()
-
-
myConnection.Close()
-
cmdClearTXT()
-
myConnection.Close()
-
Catch ex As System.Exception
-
MessageBox.Show(ex.Message)
-
End Try
-
End Sub
-
Call the sub as below; -
Call chkGunIn("insert_badge_details_here")
-
Thanks Sashi for the response. I copied and pasted your code and am still getting the same error... Invalid column name 'TNJCL'. TNJCL is the string content of "frmMain.txtBadge.text.
Are the column names correct?
Implement the below changes; -
Sub chkGunIn(byVal searchGunIn as string, byVal strBadge as String)
-
-
Call chkGunIn("first_parameter","second_parameter")
-
Thanks Sashi. There still is a problem. It runs without an exception but will not update. When I check it at the break the " Trim(strBadge)" is "Batt_In" Should I not be looking for the contents of frmMain.txtBadge.Text? I am so confused! I really thank you for your help.
This is what I have..... - Call chkGunIn("Gun_In", "Batt_In")
And.... - Sub chkGunIn(ByVal searchGunIn As String, ByVal strBadge As String)
-
-
-
Dim daGun As New SqlDataAdapter
-
Dim dsGun As New System.Data.DataSet
-
' Dim searchGunIn As String = ""
-
Dim mySelectQuery As String = ""
-
-
searchGunIn = DateTime.Now
-
-
mySelectQuery = ""
-
mySelectQuery = ("UPDATE RF_Assign SET Gun_In = '" _
-
& searchGunIn & "' ,Batt_In = '" & searchGunIn _
-
& "' WHERE Badge = " & Trim(strBadge))
-
-
Dim myConnection As New SqlConnection(connectionString)
-
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
-
Try
-
-
myConnection.Open()
-
daGun.SelectCommand = myCommand
-
daGun.Fill(dsGun)
-
myCommand.ExecuteNonQuery()
-
-
myConnection.Close()
-
cmdClearTXT()
-
myConnection.Close()
-
Catch ex As System.Exception
-
MessageBox.Show(ex.Message)
-
End Try
-
End Sub
Yes the column names are correct. Here is the insert statement. - insertStatement = "INSERT INTO RF_Assign (Autoi, Badge, Gun_Number, Gun_Out, Gun_In, Batt_Number, Batt_Out, Batt_In) VALUES ('" + Row_count + "','" _
-
+ frmMain.txtBadge.Text + "','" + frmMain.TxtGun.Text + _
-
"','" + GOut + "','" + GIn + "','" + frmMain.txtBatt.Text + "','" + BOut + "','" + BIn + "')"
-
This is the contents of mySelectQuery =
UPDATE RF_Assign SET Gun_In = '2/15/2010 9:53:58 AM' ,Batt_In = '2/15/2010 9:53:58 AM' WHERE Badge = Batt_In
Pardon me for barging in...
When I check it at the break the " Trim(strBadge)" is "Batt_In" Should I not be looking for the contents of frmMain.txtBadge.Text?
The only way your string "strBadge" is going to return a live result of the contents of "frmMain.textBadge.Text is if strBadge is a property and the 'get' method gets that text. - // C#
-
string strBadge
-
{
-
get
-
{
-
return frmMain.textBadge.Text;
-
}
-
set
-
{
-
frmMain.textBadge.Text = value;
-
}
-
}
Constantly referring directly to the control on a form is fraught with problems, not the least of which is maintainability. - insertStatement = "INSERT INTO RF_Assign (Autoi, Badge, Gun_Number, Gun_Out, Gun_In, Batt_Number, Batt_Out, Batt_In) VALUES ('" + Row_count + "','" _
-
+ frmMain.txtBadge.Text + "','" + frmMain.TxtGun.Text + _
-
"','" + GOut + "','" + GIn + "','" + frmMain.txtBatt.Text + "','" + BOut + "','" + BIn + "')"
If you have 100 queries like this and you make a change to the form then you have to update 100 places. If you use a property then you only have to change 1.
I'm not familiar with C# at all and very novice with .net. I can make "strBadge" Public to = "frmMain.txtBadge.text". I understand that is just good practice and will keep it in mind when cleaning this up. So here's the code as of right now: - Call chkGunIn("Gun_In", "Batt_In")
- Sub chkGunIn(ByVal searchGunIn As String, ByVal strBadge As String)
-
-
-
Dim daGun As New SqlDataAdapter
-
Dim dsGun As New System.Data.DataSet
-
' Dim searchGunIn As String = ""
-
Dim mySelectQuery As String = ""
-
-
searchGunIn = DateTime.Now
-
strBadge = frmMain.txtBadge.Text
-
mySelectQuery = ""
-
mySelectQuery = ("UPDATE RF_Assign SET Gun_In = '" _
-
& searchGunIn & "' ,Batt_In = '" & searchGunIn _
-
& "'WHERE Badge = " & strBadge)
-
-
Dim myConnection As New SqlConnection(connectionString)
-
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
-
Try
-
-
myConnection.Open()
-
daGun.SelectCommand = myCommand
-
daGun.Fill(dsGun)
-
myCommand.ExecuteNonQuery()
-
-
myConnection.Close()
-
cmdClearTXT()
-
myConnection.Close()
-
Catch ex As System.Exception
-
MessageBox.Show(ex.Message)
-
End Try
-
End Sub
mySelectQuery Returns the proper string on BREAK: "UPDATE RF_Assign SET Gun_In = '2/15/2010 1:15:40 PM' ,Batt_In = '2/15/2010 1:15:40 PM'WHERE Badge = TN1002" But when gets to the daGun."Fill(dsGun)" it goes to the exception with: Invalid column name 'TN1002'.
Does any of this make sense? I am still at a loss as to what is causing this problem. I had the same thing happen in another part of the code and it was the Query statement causing the issue. I just cannot figure this out.
- strBadge = frmMain.txtBadge.Text
No.
This will ONE TIME at the time the code is run set strBadge equal to the current value of frmMain.txtBadge.Text
It will not get the real-time updated value of the text box each time you use the strBadge variable. That is what a property does as I described earlier. - // C#
-
string strBadge
-
{
-
get
-
{
-
return frmMain.textBadge.Text;
-
}
-
set
-
{
-
frmMain.textBadge.Text = value;
-
}
-
}
In this example every time you use strBadge the 'get' method is executed and retreives the CURRENT value of the textbox. When you set strBadge to a new value, that value is inserted into the .Text property of the textbox
I recommend you go to the MSDN to read up on properties to get a better understanding of what I tried to describe.
Thanks tl. I understand what you are saying and in my hast in replying I did not state it clearly. But, as it is above, should this not work? Why is "mySelectQuery" showing the correct string on break, but when trying to fill: daGun."Fill(dsGun)" it takes the contents of the string and beleives it to be a column?
Sorry for the delay but I got the answer a few days back.... Hope this helps others. The problem was in the query. -
mySelectQuery = ("UPDATE RF_Assign SET Gun_In = '" _
-
& searchGunIn & "' ,Batt_In = '" & searchGunIn _
-
& "' WHERE Badge = '" & strBadge & "'")
-
Thanks for your help.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David Londeck |
last post by:
I am using Visual Basic 2003 and I have written a notepad like application
using the RichTextBox control. I am having trouble trying to emulate
Microsoft Words text block copy/paste feature. In...
|
by: Aaron Smith |
last post by:
Ok. I have a dataset that has multiple tables in it. In one of the child
tables, I have a column that I added to the DataSet (Not in the
DataSource). This column does not need to be stored in the...
|
by: mark |
last post by:
I've been looking at working with Excel data.
I understand the process of getting the data into a dataset and modifying
it. It's one of simple beauty that is well documented. Now, I want to send...
|
by: papa smerf |
last post by:
I have a list box that I want to have 3 columns of data.
For example:
FirstName, LastName, Phone, Catagory
I want to display the FirstName, LastName and Phone in the 3 columns, I will use...
|
by: Bill Nguyen |
last post by:
I tried almost everything
..Rows.clear()
..rowcount = 0
and the rows in a datagridview still not cleared
I populated the Datagridview manually using Row.Add
Any help is greatly appreciated
...
|
by: yukijocelyn |
last post by:
I have experienced a problem here while doing a form for accessing datadbase using MS Access. I'm using the AccessDataSource control, and using Gridview to pull the data from the database. I am able...
|
by: Hetal |
last post by:
Hi...
We are on Visual Basic .NET 2003 and i am using DataView to sort the
DataTable. However, we are facing problems with sorting the data based
on multiple columns.
The below statement to...
|
by: radhikabista |
last post by:
hey friends , i m not being able to save the updates in datagrid view when i press buttonsave_ gridview i have a class customer with two methods one to get the dataset and other to update database:...
|
by: =?Utf-8?B?Rmxhbm1hbg==?= |
last post by:
I have a tutorial I was working on with Visual Web Developer 2005 express.
Very basic stuff intro to asp.net. All was working fine a few days ago. I
took my pc home and installed the visual studion...
|
by: Michel Esber |
last post by:
Hi all,
DB2 V8 LUW FP 15
There is a table T (ID varchar (24), ABC timestamp). ID is PK.
Our application needs to frequently update T with a new value for ABC.
update T set ABC=? where ID...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |