How do I write an SQL statement that will show if profile record exists it should stay on the page, but if profile record DOES NOT exist if should redirect to another webpage where the profile record will be inserted?
I tried to use this but each time I go to the page it will show popup message to Please complete profile. Even if I have the profile in the database table, it will still show the message and redirect to the page where profile will be inserted.
ServerSide Code:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
Response.Redirect("login.aspx");
}
else
{
Dataprofile();
}
}
public void Dataprofile()
{
SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\spotyearDB.mdf;Integrated Security = True");
DataTable dt = new DataTable();
try
{
con.Open();
string sql = "SELECT Id, OrgName, Dept, year FROM AdminProfile WHERE Id = '" + Session["user"] + "' AND OrgName = '" + nametxt.Text + "' AND Dept = '" + deptxt.Text + "' AND year = '" + yeartxt.Text + "'";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@Id", userID.Text);
cmd.Parameters.AddWithValue("@OrgName", nametxt.Text);
cmd.Parameters.AddWithValue("@Dept", deptxt.Text);
cmd.Parameters.AddWithValue("@year", yeartxt.Text);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
Response.Redirect("ClassProfile.aspx");
deptxt.Text = ds.Tables[0].Rows[0][7].ToString();
yeartxt.Text = ds.Tables[0].Rows[0][8].ToString();
}
else
{
string message = "Please Complete your profile";
string url = "Account.aspx";
string script = "window.onload = function() {alert('";
script += message;
script += "');";
script += "window.location = '";
script += url;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}
}
catch (SqlException ex)
{
string msg = "fetch Error";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}