Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6799539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:51:38+00:00 2026-05-26T18:51:38+00:00

I was hoping someone could help me out with this stupid problem I’m having

  • 0

I was hoping someone could help me out with this stupid problem I’m having with the following SQL statement:

public void ApplyInference(string AnswerSelected)
    {
        int InferenceID;
        int QuestionID;
        string AnswerInference;
        int PainValue;
        int AnxietyValue;
        int DepressionValue;
        int FearValue;
        int TransportValue;
        int EmotionalValue;
        int FinancialValue;
        int PhysicalValue;
        int SpiritValue;
        int SocialValue;            

        SqlConnection NewConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\QuestionsDatabase.mdf;Integrated Security=True;User Instance=True"); //"Initial Catalog=Northwind;Integrated Security=SSPI");
        SqlDataReader ReadIn = null;

        try
        {
            NewConnection.Open();
            SqlCommand GetInference = new SqlCommand("SELECT * FROM InferenceDB WHERE QuestionID =" + this.QuestionID + "AND AnswerInference =" + AnswerSelected, NewConnection);
            ReadIn = GetInference.ExecuteReader();

            while (ReadIn.Read())
            {
                InferenceID = Convert.ToInt32(ReadIn[0]);
                QuestionID = Convert.ToInt32(ReadIn[1]);
                AnswerInference = Convert.ToString(ReadIn[2]);
                PainValue = Convert.ToInt32(ReadIn[3]);
                AnxietyValue = Convert.ToInt32(ReadIn[4]);
                DepressionValue = Convert.ToInt32(ReadIn[5]);
                FearValue = Convert.ToInt32(ReadIn[6]);
                TransportValue = Convert.ToInt32(ReadIn[7]);
                EmotionalValue = Convert.ToInt32(ReadIn[8]);
                FinancialValue = Convert.ToInt32(ReadIn[9]);
                PhysicalValue = Convert.ToInt32(ReadIn[10]);
                SpiritValue = Convert.ToInt32(ReadIn[11]);
                SocialValue = Convert.ToInt32(ReadIn[12]);



                MessageBox.Show("InferenceID: " + InferenceID + "\nAnswer Value: " + AnswerInference + "\nPain value: " + PainValue + "\nSocial value: " + SocialValue);
                //LoadQuestionForm(this.FormStyle);
            }
        }
        finally
        {
            if (ReadIn != null)
            {
                ReadIn.Close();
            }

            if (NewConnection != null)
            {
                NewConnection.Close();
            }
        }
    }` 

Now the code works tor every other column in the table except for the one I need which is the AnswerInference one. I am feeding the AnswerInference value in from another method which looks like this:

private void Answer1Button_Click(object sender, EventArgs e)
        {
            parent.ApplyInference("Ans1");
            CloseForm();
        } 

Unfortunately I can’t get the code to work using string data found in the table I’m using. I know this should be an easy fix, but I can for the life of me work out what’s going on. Can someone suggest what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:51:38+00:00Added an answer on May 26, 2026 at 6:51 pm

    You need a space between the question-id and the and, but more importantly: you should use parameters. Look into “SQL injection”, query-plan re-use, etc. The most appropriate way to do this is with a command like:

    using(var GetInference = new SqlCommand(@"
    SELECT *
    FROM InferenceDB
    WHERE QuestionID = @questionID AND AnswerInference = @answerInference"
           , NewConnection);
    {
        GetInference.Parameters.AddWithValue("questionID", QuestionID);
        GetInference.Parameters.AddWithValue("answerInference", AnswerSelected);
        ....
    }
    

    Also – that complex-looking try/finally can be simplified with using here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was hoping someone could help me with a little issue I'm having trouble
I am having a problem with LINQ and I was hoping someone could explain
I'm not seeing the error and was hoping someone could figure it out: public
I'm having the most peculiar problem, and I was hoping I someone could point
I'm new to rails and heroku and was hoping someone could help me out
I was hoping someone could help me figure out how to change the setmessage
hoping someone can help me out with this. I'm trying to figure out whether
I was hoping someone could help me figure out why my application keeps timing
I'm having trouble with one particular issue, I was hoping someone could help me
I was hoping someone could help me with figuring out how to POST to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.