ASP.NET C# Query

I have an inherited project in C# which I am not familiar with AT ALL.

Instead of I really need for this to read and set a variable = "eng".
How do I do that with the code below? Do I HAVE to set a dataset?


        private void QueryDatabase2()
        {
            strSQL99 = @"SELECT eng FROM tblWelcome WHERE category = 'oceanfront'";
            string conString = @"Data Source=data.com;Database=database;UID=user;PWD=password;
            SqlConnection myConn = new SqlConnection(conString);

            SqlDataAdapter oda = new SqlDataAdapter(strSQL99, myConn);

            ds = new DataSet();

            oda.Fill(ds, "tblWelcome");
            myConn.Close();
            DataList1.DataBind();

        }
Larry Bristersr. DeveloperAsked:
Who is Participating?
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.

TimCotteeHead of Software ServicesCommented:
Try this assuming that the query returns one (and only one) result.
        private void QueryDatabase2()
        {
            strSQL99 = @"SELECT eng FROM tblWelcome WHERE category = 'oceanfront'";
            string conString = @"Data Source=data.com;Database=database;UID=user;PWD=password;
            SqlConnection myConn = new SqlConnection(conString);
 
            SqlCommand cmd = new SqlCommand(strSQL99, myConn);
            
            String eng = cmd.ExecuteScalar();
            myConn.Close();
            cmd.Close();
 
        }

Open in new window

0
Larry Bristersr. DeveloperAuthor Commented:
TimCottee;
  Geting the error message below...

Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
0
TimCotteeHead of Software ServicesCommented:
This is one of those differences between vb.net and c# that throw me every so often:

String eng = (String)cmd.ExecuteScalar();
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Larry Bristersr. DeveloperAuthor Commented:
TimCottee;
  Almost there...
I too get messed up in C# and start going in circles...

This message came...

CS0117: 'System.Data.SqlClient.SqlCommand' does not contain a definition for 'Close'
0
TimCotteeHead of Software ServicesCommented:
Of course, the SqlCommand object doesn't have a Close() method, it has a Dispose() method though:

           myConn.Close();
            cmd.Dispose();

just to make sure we cleanup the objects nicely.
 
0
Larry Bristersr. DeveloperAuthor Commented:
TimCottee,
  Now I'm getting the error below....

Maybe...
Do you have a simple example of a query that includes the datasource that populates a label that I could start from?  Maybe that's the way to go and I'll just slog through unless you see something in the error below.


ExecuteScalar requires an open and available Connection. The connection's current state is closed.
0
TimCotteeHead of Software ServicesCommented:
I knew that one was coming:

Let's try and combine everything so far (and forgive my not thinking straight today). And a new thought that you might want this as a function to return the actual value of eng.


        private String QueryDatabase2()
        {
            strSQL99 = @"SELECT eng FROM tblWelcome WHERE category = 'oceanfront'";
            string conString = @"Data Source=data.com;Database=database;UID=user;PWD=password;
            SqlConnection myConn = new SqlConnection(conString);
            myConn.Open();
 
            SqlCommand cmd = new SqlCommand(strSQL99, myConn);
            
            String eng = (String)cmd.ExecuteScalar();
            myConn.Close();
            cmd.Dispose();
            return eng;
        }

Open in new window

0
Larry Bristersr. DeveloperAuthor Commented:
TomCottee;
Now I know why I stay in VB...

Anyway...
getting this now...

Not sure where the void is coming from...whene I run the select statement I get the expected results.

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0127: Since appcodesource.EstateTypes.QueryDatabase2()' returns void, a return keyword must not be followed by an object expression

Source Error:

 

Line 1052:            myConn.Close();
Line 1053:            cmd.Dispose();
Line 1054:            return eng;
Line 1055:
Line 1056:        }
 
0
Larry Bristersr. DeveloperAuthor Commented:
TimCottee:
  You put me on the right track.

eng = cmd.ExecuteScalar().ToString();

Points being awarded now....
0
Larry Bristersr. DeveloperAuthor Commented:
See my final comment
0
Mapy123Commented:
Hello,

Related to this solution, I just have one question.. first I´ll show you my code:

        private void ShowCurriculo()
        {
            string User = (String)Session["CurrentUser"];
            string strSQL = @"SELECT Titulo, [Apellido Paterno], [Apellido Materno], Direccion FROM Curriculo WHERE UserID = '" + User.ToString() + "'";
            string conString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\empleo.mdf;Integrated Security=True;User Instance=True";
            SqlConnection myConn = new SqlConnection(conString);
            myConn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, myConn);
            String DataCurriculo = (String)cmd.ExecuteScalar();
            if (DataCurriculo == null)
            {
                LabelMensajeSinCurriculo.Text = ("Usted no ha cargado su curriculo aún, le invitamos a cargarlo, seleccione Menu|Candidatos|Cargar Currículo!!");
            }
            else
            {
                LabelMuestraTitulo.Text = DataCurriculo;
                LabelMuestraApellidoPaterno.Text = DataCurriculo;
                LabelMuestraApellidoMaterno.Text = DataCurriculo;
                LabelMuestraDireccion.Text = DataCurriculo;
            }
            myConn.Close();
            cmd.Dispose();
        }

I am storing the data on my DataCurriculo variable, but I don´t know how to extract each one of the values (Titulo, [Apellido Paterno], [Apellido Materno] and then assign them to their specific LabelText.

Thank you
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
.NET Programming

From novice to tech pro — start learning today.