0

I am creating a search function in c# by referring to a tutorial in Youtube. But I have errors in certain areas of my code. Below are the codes for reference:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace WebApplication4

{
    public partial class WebForm1 : System.Web.UI.Page
    {
    MySqlConnection old = new MySqlConnection("Data Source=localhost; Database=student_record; User ID=root; Password=rootroot");

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        MySqlCommand latest = new MySqlCommand(@"SELECT * FROM stud_record WHERE (stud_id LIKE '%' + @search = '%')", old);
        latest.Parameters.AddWithValue("@search", SqlDbType.NVarChar = TextBox1.Text);
        SqlDataReader dr;


        old.Open();
        latest.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = latest;
        DataSet ds = new DataSet();
        da.Fill(ds, "stud_id");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        old.Close();
    }
}
}

The first error is "SqlDbType.NVarChar" with an error of "The left-hand side of an assignment should be a variable , property and indexer" so I assume it should be like this "NVarChar.SqlDbType" but when I did that, it states that the NVarChar does not exist in the current context.

The second error is "da.SelectCommand = latest();" it states that Cannot implicitly convert type 'MySql.Data.MySqlClient.MySqlCommand' to 'System.Data.SqlClient.SqlCommand'.

I can't seem to figure the solution. I would appreciate if someone could help me with this. Thank you very much.

  • Use AddWithValue("@search",TextBox1.Text). Not sure why you pass a SqlDbType as param. See [this answer](http://stackoverflow.com/questions/13580993/mysqlcommand-command-parameters-add-is-obsolete?answertab=votes#tab-top). For second error please provide the code. – Jehonathan Thomas Nov 20 '15 at 03:40

1 Answers1

0

The left-hand side of an assignment should be a variable , property and indexer

SqlDbType.NVarChar = TextBox1.Text this does not make sense logically and technically, whatever. You are assigning text box value to an enum constant. Also you have mixed up two ways of setting db command parameters. You should either do:

latest.Parameters.Add("@search", MySqlDbType.NVarChar);
latest.Parameters["@search"].Value = TextBox1.Text;

Or

latest.Parameters.AddWithValue("@search", TextBox1.Text);

Cannot implicitly convert type 'MySql.Data.MySqlClient.MySqlCommand' to 'System.Data.SqlClient.SqlCommand'

You are assigning MySqlCommand object to SqlDataAdapter. Why are you mixing two data providers. Whatever library you are using as mysql connector, it must be having mysql specific type for data adapter, say MySqlDataAdapter, like you have used MySqlConnection and MySqlCommand.

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32