Need help with updating and saving values to MS access from asp.net c# web form

ben lancaster 1 Reputation point
2022-02-09T11:34:45.323+00:00

I'm trying to save a transaction to a transaction table in MS database. I get the following error.

System.Data.OleDb.OleDbException
HResult=0x80040E14
Message=Syntax error (missing operator) in query expression '6GTPY'.
Source=App_Web_zys52can
StackTrace:
at _Default.SaveTransaction() in C:\Users\ben-b\Documents\NewBlackCatDatabase\NewBlackCatPCDatabase\Inventory.aspx.cs:line 63
at _Default.Button4_Click(Object sender, EventArgs e) in C:\Users\ben-b\Documents\NewBlackCatDatabase\NewBlackCatPCDatabase\Inventory.aspx.cs:line 44

This exception was originally thrown at this call stack:
[External Code]
_Default.SaveTransaction() in Inventory.aspx.cs
_Default.Button4_Click(object, System.EventArgs) in Inventory.aspx.cs
[External Code]

Webform Screenshot -

172592-inventory.jpg

The code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
OleDbConnection connection = DatabaseConnection.ConnectCurrentDatabase();
protected void Page_Load(object sender, EventArgs e)
{

}  


protected void Button1_Click1(object sender, EventArgs e)  
{  
    Response.Redirect("CurrentStock.aspx");  
}  

protected void Button2_Click(object sender, EventArgs e)  
{  
    Response.Redirect("Default.aspx");  
}  

protected void Button4_Click(object sender, EventArgs e)  
{  
    SaveTransaction();  
    GridView1.DataBind();  
    DetailsView3.DataBind();  
}  

void SaveTransaction()  
{  
    string Transaction = TextBox4.Text;  
    string ItemTypeCode = TextBox3.Text;  
    string StockIn = TextBox1.Text;  
    string StockOut = TextBox2.Text;  
    string LoggedinUserName = LoginName1.Page.User.Identity.Name;  

    connection.Open();  

    String myQuery = "Insert into TransactionTable (ItemTypeCode, Transactions, StockIn, StockOut, RecordedBy)" +  
        " Values ( " + ItemTypeCode + ",' " + Transaction + ",'" + StockIn + ",'" + StockOut + ",'" + LoggedinUserName + "')";  

    OleDbCommand command = new OleDbCommand(myQuery, connection);  
    command.ExecuteNonQuery();  

    connection.Close();  
}  
}  

The database:

172555-access-table.jpg

Will really appreciate the help as I've been trying to resolve this for 5 weeks now.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,298 questions
Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
830 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,071 Reputation points
    2022-02-10T05:38:52.3+00:00

    Hi @ben lancaster ,
    Your problem is the query. What is "6GTPY"? I need know which lines with errors.

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments