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.