I have a SQL database I have been trying to write code that one textbox value = 192.168.1.1 and another textbox value = Main Area then i need it to check in SQL for those values in a record with matching values if it finds matching values then show message that values exist. If it doesn't find those 2 values then let the user insert the new values. this is what I have so far. It does find the IPaddress but never looks to see if the area is a mach in the database. If i change to an ip address that is not in the database I get 'Object reference not set to an instance of an object.' Thanks for any help i can get on this.
C#:
private void Insert_Button_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MainConn"].ConnectionString);
conn.Open();
string IPAddressQuery = "select ipaddress FROM Computer where IPAddress='" + IP_Address_TextBox.Text + "'";
SqlCommand com = new SqlCommand(IPAddressQuery, conn);
string ipaddress = com.ExecuteScalar().ToString();
conn.Close();
if (ipaddress == IP_Address_TextBox.Text)
label4.Text =ipaddress.ToString();
MessageBox.Show("Ip Address Found");
{
conn.Open();
string checkAreaQuery = "select area from Computer where Area='" + Area_TextBox.Text + "'";
SqlCommand areaComm = new SqlCommand(checkAreaQuery, conn);
string area = areaComm.ExecuteScalar().ToString();
if (area == Area_TextBox.Text)
{
MessageBox.Show("IP Address is already in use for Area");
}
else
{
Let the user insert a new record
}
}
}
Attachments
Last edited by a moderator: