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();

        }
lrbristerAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TimCotteeConnect With a Mentor Commented:
This is one of those differences between vb.net and c# that throw me every so often:

String eng = (String)cmd.ExecuteScalar();
0
 
TimCotteeCommented:
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
 
lrbristerAuthor Commented:
TimCottee;
  Geting the error message below...

Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
lrbristerAuthor 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
 
TimCotteeCommented:
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
 
lrbristerAuthor 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
 
TimCotteeCommented:
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
 
lrbristerAuthor 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
 
lrbristerAuthor Commented:
TimCottee:
  You put me on the right track.

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

Points being awarded now....
0
 
lrbristerAuthor 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
All Courses

From novice to tech pro — start learning today.