How to fix the errors: 1. CS0201 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement. 2. CS8389 Omitting the type argument is not allowed in the current context

BenTam 1,801 Reputation points
2024-04-13T15:02:38.7133333+00:00

Dear all,

I'm a beginner in C#. Could anybody tell me how to fix the errors?

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace NewTims
{
    public partial class Student_Form : Form
    {
        public int Conn;
        public Student_Form()
        {
            InitializeComponent();
            RetrieveData();
        }
        private void RetrieveData()
        {
            string cStudentID = "0";
            string cEngName = " ";
            string cConnectionString = "Data Source=(local); Initial Catalog=Tims; Integrated Security=True";
            string SelectQuery = @"select s.*, g.EngName, c.SubjectID"
                + " from student s"
                + " left Join student g on s.memberOf = g.studentid"
                + " left join studentCourse c on s.studentid = c.studentid"
                + " left join course o on c.CourseID = o.CourseID"
                + " where s.StudentID = " + cStudentID
                + " order by s.StudentID";
            using (SqlConnection connection = new SqlConnection(cConnectionString))
            {
                string PreSelectQry = "Select top 1 StudentID from student order by " + ?cStudentID=="0","StudentID":"EngName, StudentID";
                SqlCommand command = new SqlCommand(PreSelectQry, connection);
                SqlDataAdapter adapter = new SqlDataAdapter(PreSelectQry);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "Student");
                command = new SqlCommand(SelectQuery, connection);
                adapter = new SqlDataAdapter(command);
                ds = new DataSet();
                adapter.Fill(ds, "Student");
                DataTable dt = ds.Tables["Student"];
                if (dt.Rows.Count > 0)
                {
                    DataRow r = dt.Rows[0];
                    cEngName = r.Field<string>("EngName").Trim();
                    string Sex = r.Field<string>("Sex");
                    if (Sex.Trim<> {"O"})
                    {
                        int iFirstBlank = cEngName.IndexOf(" ", cEngName.Length);
                        Surname_textBox.Text = cEngName.Substring(0, iFirstBlank);
                        GivenName_textBox.Text = cEngName.Substring(iFirstBlank);
                    }
                    else
                    {
                    }
                    Chinese_textBox.Text = r.Field<string>("Chiname");
                    StuID_textBox.Text = string.Format("{0}", r.Field<int>("StudentID").ToString("000000"));
                    HKIDCard_textBox.Text = r.Field<string>("IdCard");
                    TelEmail_textBox.Text = r.Field<string>("TelEmail");
                    MemberOf_xtextBoxButton.MyText = r.Field<string>("g.EngName");
                    StuID_textBox.Text = r.Field<string>("s.StudentID");
                    cStudentID = StuID_textBox.Text;
                    cEngName = Surname_textBox.Text.Trim() + " " + GivenName_textBox.Text.Trim();
                }
                else
                {
                    // Handle case when no data is returned
                }
            }
        }
    }
}

Error 1

enter image description here

Error 2

Error2

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2024-04-15T08:06:47.53+00:00

    Hi @BenTam-3003 , Welcome to Microsoft Q&A,

    CS1503 Argument 1: cannot convert from 'string' to 'System.Data.SqlClient.SqlCommand'

    You need to modify it like below:

     string PreSelectQry = "Select top 1 StudentID from student order by " + (cStudentID == "0" ? "StudentID" : "EngName, StudentID");
     SqlCommand command = new SqlCommand(PreSelectQry, connection);
     SqlDataAdapter adapter = new SqlDataAdapter(command);
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 81,981 Reputation points Volunteer Moderator
    2024-04-13T15:55:47.6633333+00:00

    Error 1. The syntax for ternary operator is <cond> ? <true value> : <false value>

    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator

    Error 2.

    • not equal is !=
    • .Trim is a method and requires (), to call, else it’s the method reference..
    • {“0”} is not valid syntax by itself, typically used a array initialization, but you comparing a string, so it should just be “0”

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.