B
bthumber
My application creates hostnames in sequence. Users can delete hostnames. Too
late for soft deletes. After the app creates a hostname it checks to see if
the created name and the one in the database are the same. If so, increment
the count and check again. But, it only checks that record, I need to kept
checking and incrementing till I hit a null row. How do you do that? Here is
my code below.
private void InsertRecord(string host, DateTime dateadded, int requestID,
string smc, string serialNum, string description, string firstname, string
lastname)
{
string dbHost = lblHostname.Text;
dateadded = DateTime.Now;
if (Session["Inserted"] == null)
{
string sql = "SELECT * FROM HostName WHERE Host='" + dbHost + "'";
using (SqlConnection cn = new
SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
{
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
lblInDb.Text = Convert.ToString(dr["Host"]);
}
}
else
{
lblInDb.Text = "";
}
Session["inDb"] = lblInDb.Text;
}
int count = Convert.ToInt32(Session["hostCount"]);
while (lblHostname.Text.Equals(lblInDb.Text)) // while equal
{
if (lblHostname.Text.Equals(lblInDb.Text))
{
count ++;
lblSubStr.Text = Convert.ToString(count);
if ((count >= 0) && (count <= 98)) // This was 99
{
SequenceNumbers sn = new SequenceNumbers();
lblSubStr.Text = sn.GetNumberSequence(count);
}
else if ((count >= 0) && (count <= 233)) // was ((count
if (count > 99)
{
count = count - 100;
count++;
AlphaNumberSeq ans = new AlphaNumberSeq();
lblSubStr.Text = ans.GetAlphaNumberSeq(count);
}
}
else if (count >= 333) // was 234
{
count = count - 334;
count++;
AlphaAlphaSeq aas = new AlphaAlphaSeq();
lblSubStr.Text = aas.IntToChar(count);
}
IncrementedHostSequence();
host = lblHostname.Text;
lastname = Session["lastname"].ToString();
firstname = Session["firstname"].ToString();
if (Session["nPage"] != null)
{
smc = Session["NSMC"].ToString();
serialNum = Session["Serial"].ToString();
if (Session["Ndescript"] == null)
{
description = "N/A";
}
else
{
description = Session["Ndescript"].ToString();
}
}
if (Session["sPage"] != null)
{
smc = Session["sSCen"].ToString();
serialNum = Session["sSerialNum"].ToString();
if (Session["serdescript"] == null)
{
description = "N/A";
}
else
{
description = Session["serdescript"].ToString();
}
}
if (Session["sdPage"] != null)
{
smc = Session["sdsmc1"].ToString();
serialNum = Session["sdSerialNumber"].ToString();
if (Session["storDescript"] == null)
{
description = "N/A";
}
else
{
description = Session["storDescript"].ToString();
}
}
if (Session["wsPage"] != null)
{
if (Session["workDescript"] == null)
{
description = "N/A";
}
else
{
description = Session["workDescript"].ToString();
}
smc = Session["wsSrvce"].ToString();
}
SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand cmd = new SqlCommand("INSERT INTO [HostName]
([Host], [DateAdded], [HostNameRequestID], [SMCContact], [DeviceSerialNum],
[Description], [UserFirstName], [UserLastName]) VALUES (@Host, @DateAdded,
@HostNameRequestID, @SMCContact, @DeviceSerialNum, @Description,
@UserFirstName, @UserLastName)", conn);
cmd.Parameters.AddWithValue("Host", host);
cmd.Parameters.AddWithValue("DateAdded", dateadded);
cmd.Parameters.AddWithValue("HostNameRequestID",
requestID);
cmd.Parameters.AddWithValue("SMCContact", smc);
cmd.Parameters.AddWithValue("DeviceSerialNum", serialNum);
cmd.Parameters.AddWithValue("Description", description);
cmd.Parameters.AddWithValue("UserFirstName", firstname);
cmd.Parameters.AddWithValue("UserLastName", lastname);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
lblMsgErr.Visible = true;
lblMsgErr.Text += "<br><b> Possible duplicate IP
address </b> " + err.Message;
}
finally
{
if (conn != null) { conn.Close(); }
}
}
} // End of first loop while equal.
late for soft deletes. After the app creates a hostname it checks to see if
the created name and the one in the database are the same. If so, increment
the count and check again. But, it only checks that record, I need to kept
checking and incrementing till I hit a null row. How do you do that? Here is
my code below.
private void InsertRecord(string host, DateTime dateadded, int requestID,
string smc, string serialNum, string description, string firstname, string
lastname)
{
string dbHost = lblHostname.Text;
dateadded = DateTime.Now;
if (Session["Inserted"] == null)
{
string sql = "SELECT * FROM HostName WHERE Host='" + dbHost + "'";
using (SqlConnection cn = new
SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
{
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
lblInDb.Text = Convert.ToString(dr["Host"]);
}
}
else
{
lblInDb.Text = "";
}
Session["inDb"] = lblInDb.Text;
}
int count = Convert.ToInt32(Session["hostCount"]);
while (lblHostname.Text.Equals(lblInDb.Text)) // while equal
{
if (lblHostname.Text.Equals(lblInDb.Text))
{
count ++;
lblSubStr.Text = Convert.ToString(count);
if ((count >= 0) && (count <= 98)) // This was 99
{
SequenceNumbers sn = new SequenceNumbers();
lblSubStr.Text = sn.GetNumberSequence(count);
}
else if ((count >= 0) && (count <= 233)) // was ((count
{= 100) && (count <= 233))
if (count > 99)
{
count = count - 100;
count++;
AlphaNumberSeq ans = new AlphaNumberSeq();
lblSubStr.Text = ans.GetAlphaNumberSeq(count);
}
}
else if (count >= 333) // was 234
{
count = count - 334;
count++;
AlphaAlphaSeq aas = new AlphaAlphaSeq();
lblSubStr.Text = aas.IntToChar(count);
}
IncrementedHostSequence();
host = lblHostname.Text;
lastname = Session["lastname"].ToString();
firstname = Session["firstname"].ToString();
if (Session["nPage"] != null)
{
smc = Session["NSMC"].ToString();
serialNum = Session["Serial"].ToString();
if (Session["Ndescript"] == null)
{
description = "N/A";
}
else
{
description = Session["Ndescript"].ToString();
}
}
if (Session["sPage"] != null)
{
smc = Session["sSCen"].ToString();
serialNum = Session["sSerialNum"].ToString();
if (Session["serdescript"] == null)
{
description = "N/A";
}
else
{
description = Session["serdescript"].ToString();
}
}
if (Session["sdPage"] != null)
{
smc = Session["sdsmc1"].ToString();
serialNum = Session["sdSerialNumber"].ToString();
if (Session["storDescript"] == null)
{
description = "N/A";
}
else
{
description = Session["storDescript"].ToString();
}
}
if (Session["wsPage"] != null)
{
if (Session["workDescript"] == null)
{
description = "N/A";
}
else
{
description = Session["workDescript"].ToString();
}
smc = Session["wsSrvce"].ToString();
}
SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand cmd = new SqlCommand("INSERT INTO [HostName]
([Host], [DateAdded], [HostNameRequestID], [SMCContact], [DeviceSerialNum],
[Description], [UserFirstName], [UserLastName]) VALUES (@Host, @DateAdded,
@HostNameRequestID, @SMCContact, @DeviceSerialNum, @Description,
@UserFirstName, @UserLastName)", conn);
cmd.Parameters.AddWithValue("Host", host);
cmd.Parameters.AddWithValue("DateAdded", dateadded);
cmd.Parameters.AddWithValue("HostNameRequestID",
requestID);
cmd.Parameters.AddWithValue("SMCContact", smc);
cmd.Parameters.AddWithValue("DeviceSerialNum", serialNum);
cmd.Parameters.AddWithValue("Description", description);
cmd.Parameters.AddWithValue("UserFirstName", firstname);
cmd.Parameters.AddWithValue("UserLastName", lastname);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
lblMsgErr.Visible = true;
lblMsgErr.Text += "<br><b> Possible duplicate IP
address </b> " + err.Message;
}
finally
{
if (conn != null) { conn.Close(); }
}
}
} // End of first loop while equal.