locked
Unable to insert data into another column based on the unique ID RRS feed

  • Question

  • User1637251936 posted

    Hello, I am trying to insert new data into an empty column based on the matched unique ID found. There is only one table which contain three columns.

    For example, in the 1st row, there are two filled column which contain unique ID and name, this data is stored in 1st source page, the third column is still empty in the same row and the function to add data to this column is in 2nd source page. I cannot combine the source pages as each page have many functions.

    I am able to transfer the unique id to the other page but when I am trying to insert the new data based on the unique ID it gives error that says:

    Query input must contain at least one table or query.

    This is my code to insert new data with existing unique ID from previos source page:

     Sentence:<asp:TextBox runat="server" ID="sentence"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Click" OnClick="Button1_Click" />
    
    _______________________________________________________________________________________________
    
    
    string userID;
        string ConnectString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA SOURCE=" + @"D:\\Test.accdb;";
        OleDbConnection objCon;
        OleDbCommand objCmd;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            userID = Request.QueryString["Id"];
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
           string SqlString = "Insert Into Table1 (Sentence) Values (@sentence) where UniqueID= @userID";
    
           using (objCon = new OleDbConnection(ConnectString))
           {
               using(objCmd = new OleDbCommand(SqlString, objCon))
               {
                   objCmd.Parameters.AddWithValue("@sentence", sentence.Text);
                   objCon.Open();
                   objCmd.ExecuteNonQuery();
               }
           }
           
        }

    Any ideas, help and suggestions are greatly appreciated, thank you.


    Thursday, October 3, 2013 7:58 AM

Answers

  • User-1199946673 posted

    collo

    should I use something like this?
    select Sentence from Table1 where UniqueID like '%" + Label1.Text + "%'

    This is a SELECT statement, the previous one was an INSERT statement. It's not clear what you are trying to do? Show us your table structure, some sample data and the result you expect?

    By the way, The above code is vulnarable to SQL injections. always use parameterized queries:

    http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 5, 2013 2:02 AM

All replies

  • User824124681 posted

       string SqlString = "Insert Into Table1 (Sentence) Values (@sentence) where UniqueID= @userID";
    
           using (objCon = new OleDbConnection(ConnectString))
           {
               using(objCmd = new OleDbCommand(SqlString, objCon))
               {
                   objCmd.Parameters.AddWithValue("@sentence", sentence.Text);
                   objCon.Open();
                   objCmd.ExecuteNonQuery();
               }
           }

    You are missing @userID parameter

    Thursday, October 3, 2013 11:11 AM
  • User1637251936 posted

    should I add something like this??

    objCmd.Parameters.AddWithValue("@UniqueID", userID);



    Thursday, October 3, 2013 9:29 PM
  • User-1199946673 posted
    string SqlString = "Insert Into Table1 (Sentence) Values (@sentence) where UniqueID= @userID";

    Take a closer look to your insert statement. the Where Clause doesn't make any sense....


     

     

    Friday, October 4, 2013 7:21 AM
  • User1637251936 posted

    should I use something like this?

    select Sentence from Table1 where UniqueID like '%" + Label1.Text + "%'



    Friday, October 4, 2013 10:37 PM
  • User-1199946673 posted

    collo

    should I use something like this?
    select Sentence from Table1 where UniqueID like '%" + Label1.Text + "%'

    This is a SELECT statement, the previous one was an INSERT statement. It's not clear what you are trying to do? Show us your table structure, some sample data and the result you expect?

    By the way, The above code is vulnarable to SQL injections. always use parameterized queries:

    http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 5, 2013 2:02 AM