Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

ASP.NET C# Code

I can get the text below to populate my output.

How do I get the "eng" to populate with the data from the SQl Server?

It's always going to be a single record and a single field that's returned.

 private void QueryDatabase2()
        {
            strSQL99 = @"SELECT eng FROM tblCategoryWelcome WHERE category = 'oceanfront'";
            string conString = @"Data Source=mydata.com;Database=db1;UID=user;PWD=password";
            SqlConnection myConn = new SqlConnection(conString);
 
            SqlCommand cmd = new SqlCommand(strSQL99, myConn);
           
            eng = "Data Here";
            myConn.Close();
            //cmd.Close();
 
        }
Avatar of pollock_d
pollock_d

go
myConn.Open();
eng = cmd.ExecuteQuery();
myConn.Close();
oh no, i mean

eng = cmd.ExecuteScalar();
Avatar of Larry Brister

ASKER

pollock_d:

When I use your code I get
Compiler Error Message: CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)

Source Error:
Line 1046:            SqlCommand cmd = new SqlCommand(strSQL99, myConn);
Line 1047:            myConn.Open();
Line 1048:            eng = cmd.ExecuteScalar();
Line 1049:

 When I added  eng = cmd.ExecuteScalar().ToString;
I get
Compiler Error Message: CS0428: Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?

Source Error:
Line 1046:            SqlCommand cmd = new SqlCommand(strSQL99, myConn);
Line 1047:            myConn.Open();
Line 1048:            eng = cmd.ExecuteScalar().ToString;
Line 1049:

 
ASKER CERTIFIED SOLUTION
Avatar of pollock_d
pollock_d

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
YAY!  HOT DOG!  WHEE!  WHOOPIE!!!

uh.....

Thanks.
haha