Hi PRA,
I have created sample code which full-fill your requirement. You need to maintain your table name as per your requirement.
HTML
<div>
<asp:TextBox ID="txtName" runat="server" />
<asp:Button Text="Submit" OnClick="OnSumbit" runat="server" />
</div>
C#
private string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
protected void OnSumbit(object sender, EventArgs e)
{
int id = 0;
using (SqlConnection con = new SqlConnection(constring))
{
SqlCommand cmd1 = new SqlCommand("SELECT MAX(Id) as Id1 FROM TABLE1", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
sda.Fill(dt1);
SqlCommand cmd2 = new SqlCommand("SELECT MAX(Id) as Id2 FROM TABLE2", con);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
sda1.Fill(dt2);
if (string.IsNullOrEmpty(dt1.Rows[0]["Id1"].ToString()) && string.IsNullOrEmpty(dt2.Rows[0]["Id2"].ToString()))
{
id = 1;
SaveData(id, "TABLE1");
}
else
{
int Id2 = dt2.Rows[0]["Id2"].ToString() == "" ? 0 : Convert.ToInt32(dt2.Rows[0]["Id2"].ToString());
if (Convert.ToInt32(dt1.Rows[0]["Id1"].ToString()) > Id2)
{
id = Convert.ToInt32(dt1.Rows[0]["Id1"].ToString()) + 1;
SaveData(id, "TABLE2");
}
else
{
id = Convert.ToInt32(dt2.Rows[0]["Id2"].ToString()) + 1;
SaveData(id, "TABLE1");
}
}
}
}
private void SaveData(int id, string tableName)
{
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("INSERT INTO " + tableName + " Values(@Id,@Name)", con);
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Vb.Net
Private constring As String = ConfigurationManager.ConnectionStrings("constring").ToString()
Protected Sub OnSumbit(sender As Object, e As EventArgs)
Dim id As Integer = 0
Using con As New SqlConnection(constring)
Dim cmd1 As New SqlCommand("SELECT MAX(Id) as Id1 FROM TABLE1", con)
Dim sda As New SqlDataAdapter(cmd1)
Dim dt1 As New DataTable()
sda.Fill(dt1)
Dim cmd2 As New SqlCommand("SELECT MAX(Id) as Id2 FROM TABLE2", con)
Dim sda1 As New SqlDataAdapter(cmd2)
Dim dt2 As New DataTable()
sda1.Fill(dt2)
If String.IsNullOrEmpty(dt1.Rows(0)("Id1").ToString()) AndAlso String.IsNullOrEmpty(dt2.Rows(0)("Id2").ToString()) Then
id = 1
SaveData(id, "TABLE1")
Else
Dim Id2 As Integer = If(dt2.Rows(0)("Id2").ToString() = "", 0, Convert.ToInt32(dt2.Rows(0)("Id2").ToString()))
If Convert.ToInt32(dt1.Rows(0)("Id1").ToString()) > Id2 Then
id = Convert.ToInt32(dt1.Rows(0)("Id1").ToString()) + 1
SaveData(id, "TABLE2")
Else
id = Convert.ToInt32(dt2.Rows(0)("Id2").ToString()) + 1
SaveData(id, "TABLE1")
End If
End If
End Using
End Sub
Private Sub SaveData(id As Integer, tableName As String)
Dim con As New SqlConnection(constring)
Dim cmd As New SqlCommand((Convert.ToString("INSERT INTO ") & tableName) + " Values(@Id,@Name)", con)
cmd.Parameters.AddWithValue("@Id", id)
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim())
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
Screenshot