locked
Need to create two connection strings and fetch data from DB2.table2 if data not exists in DB1.table1 RRS feed

  • Question

  • User1182587605 posted

    I need to work on two databases, So, I need to create two connection strings. 

    Now, If cost of an item exists in DB1.Table1 I can fetch it and show in my ASP.NET textbox. But if not, I need to check for the same item in DB2.Table2 and fetch the value from there. Please give me a code sample on how to do this.

    Thanks,

    Deepak

    Saturday, November 26, 2016 7:49 PM

All replies

  • User-707554951 posted

    Hi acmedeepak,

    From your description, I suggest you could use SqlDataReader to fetch data from DB1.Tabl. because we could use dr.HasRows to check whether data exist in DB1.Tabl. if not, we could create new connectstring to connect DB2 and fetch data from table in DB2.

    Following is an sample, you could refer to it:

    Note: CategoryName='Somethingwater' row only exist in DB2( it's connectstring is "connectstring")

     protected void Page_Load(object sender, EventArgs e)
            {
                FetchData();
            }
            private void FetchData()
            {
                string query = "select * from Categories where CategoryName='Somethingwater'";
                DataTable dt = new DataTable();
                String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["Model2"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);    
                SqlCommand cmd = new SqlCommand(query,con);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
          cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        TextBox1.Text = dr.GetString(2);
                    }
                    
                }
             
                else
                {
                    con.Close();
                    string query2 = "select * from Categories where CategoryName='Somethingwater'";
                    String strConnString2 = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                    SqlConnection con2 = new SqlConnection(strConnString2);
                    SqlCommand cmd2 = new SqlCommand(query2,con2);
                    con2.Open();
                    SqlDataReader dr2 = cmd2.ExecuteReader();
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    if (dr2.HasRows)
                    {
                        while (dr2.Read())
                        {
                            TextBox1.Text = dr2.GetInt32(0)+ dr2.GetString(1)+ dr2.GetString(2);
                        }
                        con2.Close();
                    }
                    else
                    {
                        Response.Write("data not exsit in the two tables");
                    }
                }
               
            }


    Output:


     
    Best regards

    Cathy

    Monday, November 28, 2016 6:45 AM