• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

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;
0
rwheeler23
Asked:
rwheeler23
  • 4
  • 2
2 Solutions
 
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
 
rwheeler23Author Commented:
TrxState = GPDataSet.Tables["DOCNUMBR"].Rows[0].["TRXSTATE"]; just before the ["TRXSTATE"] it says Identifier Expected
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now