Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi, i am using asp.net2.0 with c#

i want to get dynamically the Data Source value from which system iam acessing the website and concatenate in connection String

but still showing null value
can you correct my code which helps me

C#
string strcon = "Initial Catalog=POS;User ID=sa;Password=103";
   protected void Page_Load(object sender, EventArgs e)
   {
       
       if (Page.IsPostBack == false)
       {
           string[] parts = strcon.Split(';');
                      
           string dataSource = "";
           for (int i = 0; i < parts.Length; i++)
           {
               string part = parts[i].Trim();
               if (part.StartsWith("Data Source="))
               {
                   dataSource = part.Replace("Data Source=", "");
                   break;
               }
           }
       }
   }
Posted
Updated 7-Jul-10 22:13pm
v2
Comments
Sandeep Mewara 8-Jul-10 4:14am    
Well, whatever you are doing doesn't make much of a sense.
Further, this is not the way to handle multiple databases! Read about them first. Playing around with connection string is not suggestible!

This looks like a nightmare. The part where your page load event is mashing up a connection string, is the part at which your code is basically unusable. Have you tried stepping through your code ? Did you even try reading it ? None of your variable starts with "Data Source=", therefore your code does nothing. You should do some googling and read up on how to use the debugger. then you should think about what on earth you thought this code was good for, anyhow. Then you should read up on n-tiered development.
 
Share this answer
 
In the connectionstring, For the database name it can be data source or it may also be server. please check for both.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900