Hi Sat,
Here i have created sample that full-fill your requirement.
HTML
<div>
<table>
<tr>
<td>
Name:<br />
<asp:TextBox ID="txtName" runat="server" />
</td>
<td>
Country:<br />
<asp:TextBox ID="txtCountry" runat="server" />
</td>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
</td>
</tr>
</table>
<br />
<asp:GridView runat="server" ID="GridView1" />
<br />
<asp:GridView runat="server" ID="GridView2" />
</div>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers GO SELECT * FROM CustomerDetails"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
GridView2.DataSource = ds.Tables[1];
GridView2.DataBind();
}
}
}
}
}
protected void Insert(object sender, EventArgs e)
{
string name = txtName.Text;
string country = txtCountry.Text;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers(Name, Country) VALUES (@Name, @Country)"))
{
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Connection = con;
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd = new SqlCommand("UPDATE CustomerDetails SET Name = @Name, Country = @Country WHERE CustomerId = 4"))
{
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Connection = con;
cmd.ExecuteNonQuery();
}
con.Close();
}
this.BindGrid();
}
Screenshot
