I trying to change database from MySQL to SQL server 2008 express for this VS2010 C# project. However after i change the connection string and queries, the program produces error 'null reference exception was unhandled' on the "cmr.close()". Here is the code and the place where the error occurred :
namespace JawiRdrSQL
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
SqlConnection sc = new SqlConnection("Data Source=USER-PC\\SQLEXPRESS;Initial Catalog=firstDB;Integrated Security=True");
SqlCommand cmd;
SqlDataReader cmr;
public MainWindow()
{
InitializeComponent();
}
//string sc;
string strValue;
private void button1_Click(object sender, RoutedEventArgs e)
{
strValue = textBox1.Text;
char[] strVal = strValue.ToCharArray();
Array.Reverse(strVal);
foreach(char obj in strVal)
{
try
{
sc.Open();
cmd = new SqlCommand ("select JawiReader (stringR)" + ((char)obj), sc);
cmd.ExecuteNonQuery();
if (cmr.Read())
{
label1.Content += cmr["stringJ"].ToString();
}
else
{
MessageBox.Show("tidak sah");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
cmr.Close(); // error occurs here
sc.Close();
}
}
I am trying to produce a program that received a string which will be break into char and then compared to values in database. After that the program will output a string of values from the database.