VS C# assign values to variables

What is the correct syntax to assign a value coming out of a SQL query using VS C# to a variable?

I am stuck on this line     TrxState = GPDataSet.Tables["DOCNUMBR"].Rows[0].["TRXSTATE"];
The other values go into text fields on the screen and they work fine. This one field, TRXSTATE, is just a variable in the program.

GPDataSet = new System.Data.DataSet();
                GPDataSet.CaseSensitive = false;

                GPDataCommand = new System.Data.SqlClient.SqlCommand();
                GPDataCommand.Connection = GPDataConnection;

                GPDataCommand.CommandText = "SELECT TRXSTATE,TRXTYPE,PADocnumber20,PADOCDT,BACHNUMB,CUSTNMBR,CUSTNAME,CSTPONBR,PACOMM FROM rbsPADocNumber WHERE PADocnumber20 = '" + PADocNumber + "'";

                GPDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                GPDataAdapter.SelectCommand = GPDataCommand;
                GPDataAdapter.TableMappings.Add("Table", "DOCNUMBER");

                GPDataAdapter.Fill(GPDataSet);
                if (GPDataSet == null || (GPDataSet.Tables.Count == 0) || (GPDataSet.Tables[0].Rows.Count == 0))
                {
                    MessageBox.Show("No documents found for this job");
                    /* Dispose of the connection */
                    GPConnObj = null;

                    /* Shutdown the connection */
                    resp = GPConnection.Shutdown();
                    GPDataConnection.Close();
                    this.Hide();
                    this.Dispose();
                }

                TrxState = GPDataSet.Tables["DOCNUMBR"].Rows[0].["TRXSTATE"];
                MessageBox.Show(TrxState);

                this.txtTrxType.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"TRXTYPE").ToString();
                this.txtTrxType.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
LVL 1
rwheeler23Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
>>What is the correct syntax to assign a value coming out of a SQL query using VS C# to a variable?

By an equals sign is often enough - as you do.  Note assigning a string to a int variable or a null to something that can't be null for example will lead to problems.

What error message do you get when you compile or run the app?
0
Fernando SotoRetiredCommented:
Are there any exceptions being thrown? If yes please post exception message and inner exception message.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rwheeler23Author Commented:
TrxState = GPDataSet.Tables["DOCNUMBR"].Rows[0].["TRXSTATE"]; just before the ["TRXSTATE"] it says Identifier Expected
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

AndyAinscowFreelance programmer / ConsultantCommented:
Misspelt name?

TrxState = GPDataSet.Tables["DOCNUMBR"].Rows[0].["TRXSTATE"];
                MessageBox.Show(TrxState);

                this.txtTrxType.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"TRXTYPE").ToString();
0
rwheeler23Author Commented:
Good catch, but no. It still says Identifier Expected.
0
rwheeler23Author Commented:
OK, here is my entire section of code. I added a text field to the form called txtTrxState. When I add the code to assign the value to this text field, all the other text fields go blank. If I remove this code, the other fields all have values. What is causing this?

        private void GetDocumentJobNumberInfo()
        {
            /* Code to get the job number from the transaction, if any */
            try
            {
                GPDataSet = new System.Data.DataSet();
                GPDataSet.CaseSensitive = false;

                GPDataCommand = new System.Data.SqlClient.SqlCommand();
                GPDataCommand.Connection = GPDataConnection;

                GPDataCommand.CommandText = "SELECT TRXSTATE,TRXTYPE,PADocnumber20,PADOCDT,BACHNUMB,CUSTNMBR,CUSTNAME,CSTPONBR,PACOMM FROM rbsPADocNumber WHERE PADocnumber20 = '" + PADocNumber + "'";

                GPDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                GPDataAdapter.SelectCommand = GPDataCommand;
                GPDataAdapter.TableMappings.Add("Table", "DOCNUMBER");

                GPDataAdapter.Fill(GPDataSet);
                if (GPDataSet == null || (GPDataSet.Tables.Count == 0) || (GPDataSet.Tables[0].Rows.Count == 0))
                {
                    MessageBox.Show("No documents found for this job");
                    /* Dispose of the connection */
                    GPConnObj = null;

                    /* Shutdown the connection */
                    resp = GPConnection.Shutdown();
                    GPDataConnection.Close();
                    this.Hide();
                    this.Dispose();
                }
               
/* This piece of code causes all the other fields to go blank */
                this.txtTrxState.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"], "TRXSTATUS").ToString();
                this.txtTrxState.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtTrxType.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"TRXTYPE").ToString();
                this.txtTrxType.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
                MessageBox.Show(this.txtTrxType.Text);

                this.txtDocNumber.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"PADocnumber20").ToString();
                this.txtDocNumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtDate.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"], "PADOCDT").ToString();
                this.txtDate.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                try
                {
                    if (this.txtDate.Text != "")
                    {
                        this.txtDate.Text = Convert.ToDateTime(this.txtDate.Text).ToString("MM/dd/yyyy");
                    }
                }
                catch (Exception ex)
                {
                    string eMsg = "001: ERROR: " + ex.Message;
                    if (StackTraceWanted) eMsg += "\n" + ex.StackTrace;
                    MessageBox.Show(eMsg);
                }

                this.txtBatchID.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"BACHNUMB").ToString();
                this.txtBatchID.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtCustID.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"CUSTNMBR").ToString();
                this.txtCustID.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtCustName.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"CUSTNAME").ToString();
                this.txtCustName.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtCustPONumber.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"CSTPONBR").ToString();
                this.txtCustPONumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtComment.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"], "PACOMM").ToString();
                this.txtComment.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
            }
            catch (Exception ex)
            {
                string eMsg = "002: ERROR: " + ex.Message;
                if (StackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }
        }
0
rwheeler23Author Commented:
Thanks for helping guys. I am just going blind. Too much staring at this screen. I had a couple of typos in my field names.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.