// This code is not solving my problem .Data redundency still exist... lblresult.Text always give 0 :(
int count=0 ;
string selectSQL = "SELECT Equipment_Name,Vendor_Name FROM CatC";
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = "Data Source=.;Initial Catalog=ecsd1;User ID=sa;Password=786";
myConn.Open();
SqlCommand cmd1 = new SqlCommand(selectSQL, myConn);
SqlDataReader reader;
try
{
myConn.Open();
reader = cmd1.ExecuteReader();
// For each item, add the author name to the displayed list box text, and store the unique ID in the Value property.
while (reader.Read())
{
String abc=reader["Equipment_Name"].ToString() ;
String fgh=reader["Vendor_Name"].ToString();
if (TextBox1.Text == abc && TextBox10.Text ==fgh )
{
lblStatus.Text = "***Data Already exist***";
count = 1;
return;
}
}
reader.Close();
}
catch (Exception err)
{
lblresult.Text = "Error reading list of References. ";
lblresult.Text += err.Message;
}
finally
{
myConn.Close();
}
lblresult.Text =count.ToString();
if (count != 1)
{
string sqlIns = "INSERT INTO CatC (Serial_No,Equipment_Name,Vendor_Name) VALUES (@Serial_No,@Equipment_Name,@Vendor_Name)";
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=ecsd1;User ID=sa;Password=786";
SqlCommand cmd = new SqlCommand(sqlIns, con);
try
{
con.Open();
cmd.Parameters.Add("@Serial_No", TextBox9.Text);
cmd.Parameters.Add("@Equipment_Name", TextBox1.Text);
cmd.Parameters.Add("@Vendor_Name", TextBox10.Text);
int ab = cmd.ExecuteNonQuery();
lblStatus.Text = "***Record Inserted.***";
if (ab == 1)
{
TextBox1.Text = "";
TextBox9.Text = "";
}
}
catch (Exception err)
{
lblStatus.Text = "***Error in inserting record***";
lblStatus.Text += err.Message;
}
finally { con.Close(); }
}
}
else if (Labelll.Text == "D. Components And Devices")
{
string sqlIns = "INSERT INTO CatD (Serial_No,Equipment_Name) VALUES (@Serial_No,@Equipment_Name)";
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=ecsd1;User ID=sa;Password=786";
SqlCommand cmd = new SqlCommand(sqlIns, con);
try
{
con.Open();
cmd.Parameters.Add("@Serial_No", TextBox9.Text);
cmd.Parameters.Add("@Equipment_Name", TextBox1.Text);
int ab = cmd.ExecuteNonQuery();
lblStatus.Text = "***Record Inserted.***";
if (ab == 1)
{
TextBox1.Text = "";
TextBox9.Text = "";
}
}
catch (Exception err)
{
lblStatus.Text = "***Error in inserting record***";
lblStatus.Text += err.Message;
}
finally { con.Close(); }