Wrote this query in MSSQL:
SELECT Code, Description,LEAD(Code, 1) OVER (ORDER BY code) AS next_code FROM Liguanea_Lane WHERE code LIKE '%88%'
Wrote back the same query in my C# code only this time it is accepting input from a comboBox name "search" and executes on the button click. This is it below:
private void button1_Click(object sender, EventArgs e)
{
try
{
string connectionString = "Data Source=JAVY26;Initial Catalog=Pharmacies;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string query = "SELECT Code, Description,LEAD(Code, 1) OVER (ORDER BY code) AS next_code FROM Liguanea_Lane WHERE code LIKE '%" + search.Text+"%'; ";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string scode = dr.GetString(dr.GetOrdinal("next_code"));
textBox2.Text = scode;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
My issue is that I keep getting: "Data is Null. This method or property cannot be called on Null Values." Is my query within my code wrong? It works fine in my SQL Server.