Change the DownloadScript method with below and check.
public void DownloadScript()
{
string iDValues = "", insertQry, updateQry;
int i = 0;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("select * from Estate_TR", con);
primaryKey = GetPrimaryKey(tableName, conString);
insertQry = "";
con.Open();
if (File.Exists(Server.MapPath("~/" + tableName + ".sql")))
{
File.Delete(Server.MapPath("~/" + tableName + ".sql"));
}
// Writing Create Table Script.
StreamWriter sw = new StreamWriter(Server.MapPath("~/" + tableName + ".sql"), true, Encoding.UTF8);
sw.Write(GenerateCreateTableScript() + Environment.NewLine);
sw.Close();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
i = i + 1;
updateAdd = "";
insertQry = "";
string celldata = "", coulmenName = "";
for (int j = 0; j < sdr.FieldCount; j++)
{
if (j > 0)
{
coulmenName += "," + sdr.GetName(j).ToString();
if (!string.IsNullOrEmpty(sdr[j].ToString()))
{
celldata += ",N'" + sdr[j].ToString() + "'";
}
else
{
celldata += ",'" + sdr[j].ToString() + "'";
}
}
else
{
coulmenName += sdr.GetName(j).ToString();
if (!string.IsNullOrEmpty(sdr[j].ToString()))
{
celldata += "N'" + sdr[j].ToString() + "'";
}
else
{
celldata += "'" + sdr[j].ToString() + "'";
}
}
if (primaryKey == sdr.GetName(j).ToString())
{
iDValues = sdr[j].ToString();
}
if (iDValues != null)
{
updateQry = UpdateQuery(coulmenName, celldata, primaryKey, iDValues);
updateAdd += updateQry;
insertQry = InsertQuery(coulmenName, celldata, tableName);
}
}
WriteScripts(tableName, insertQry, updateAdd, iDValues, primaryKey, i);
}
// Download the sql script file.
if (File.Exists(Server.MapPath("~/" + tableName + ".sql")))
{
byte[] bytes = File.ReadAllBytes(Server.MapPath("~/" + tableName + ".sql"));
File.Delete(Server.MapPath("~/" + tableName + ".sql"));
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + tableName + ".sql");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}