Hi yohana,
I have created sample code which fullfill your requirement.
HTML
Default.aspx Page
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td>
<br />
</td>
</tr>
<tr>
<td>
State:
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<br />
</td>
</tr>
<tr>
<td>
Country:
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
<br />
<asp:Button ID="btnAdd" Text="Add" OnClick="OnAdd" runat="server" />
</div>
</form>
C#
private string constr = ConfigurationManager.ConnectionStrings["Constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetCountry();
this.GetState();
if (Request.QueryString["Id"] != null)
{
GetEdit();
}
}
}
private void GetEdit()
{
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM UserDetails WHERE Id='" + Request.QueryString["Id"] + "'", con);
con.Open();
SqlDataReader idr = cmd.ExecuteReader();
if (idr.Read())
{
txtName.Text = idr["Name"].ToString();
ddlState.Text = idr["State"].ToString();
ddlCountry.Text = idr["Country"].ToString();
}
con.Close();
}
}
private void GetState()
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM State", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
ddlState.DataSource = dt;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "StateId";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("---SELECT State---"));
}
}
}
private void GetCountry()
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Country", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
ddlCountry.DataSource = dt;
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryId";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("---SELECT Country---"));
}
}
}
protected void OnAdd(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("INSERT INTO UserDetails VALUES(@Name,@State,@Country)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@State", ddlState.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Response.Redirect("Profileview.aspx");
}
HTML
Profileview.aspx Page
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="hidId" runat="server" />
<br />
Name:<asp:Label ID="lblName" Text="" runat="server" />
<br />
State:
<asp:Label ID="ddlState" Text="" runat="server" />
<br />
Country:<asp:Label ID="ddlCountry" Text="" runat="server" />
</div>
<asp:LinkButton ID="btnEdit" Text="Edit" OnClick="OnEdit" runat="server" />
</form>
C#
private string constr = ConfigurationManager.ConnectionStrings["Constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetProfile();
}
}
private void GetProfile()
{
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("SELECT Id,Name,StateName,CountryName FROM UserDetails INNER JOIN State ON UserDetails.State=StateId INNER JOIN Country ON Country .CountryID = UserDetails.Country", con);
con.Open();
SqlDataReader idr = cmd.ExecuteReader();
if (idr.Read())
{
hidId.Value = idr["Id"].ToString();
lblName.Text = idr["Name"].ToString();
ddlState.Text = idr["StateName"].ToString();
ddlCountry.Text = idr["CountryName"].ToString();
}
con.Close();
btnAdd.Text = "Update";
}
}
protected void OnEdit(object sender, EventArgs e)
{
Response.Redirect("Default.aspx?Id=" + hidId.Value);
}
Screenshot