Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi dear,

I am making a small program in ASP.NET in C# but am not able to set the label names from a Database table.


Table Name: Columns

col_id col_name
======= ==========================
Label1 City ID
Label2 City Name
Label3 City Addrs


i want to set all the lables from the table "Columns".

This function will be called at the time of Page Load()


More Details of My code "I am not Sure It is True"


In Class file Main.cs


 public static string GetColum_Des(string p_opt_id)
{
    string str = System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
  
    SqlCommand cmd;
    SqlDataReader rd;
    SqlConnection con = new SqlConnection(str);
                     
    unsafe
    {
        string str1, str2;
              
        con.Open();
        cmd = new SqlCommand("select col_id,col_name from LOCATIN_DETAIL where opt_ID=@p_opt_id ", con);
        cmd.Parameters.AddWithValue("@p_opt_id", p_opt_id);
         rd = cmd.ExecuteReader();
        while (rd.Read())
        {
            str1 = rd["col_id"].ToString() + ".Text";//I want to get Label Id from here +.Text Means "Label1.Text" in str
            str2=rd["col_name"].ToString();//I want to get Label value from here  which had stored in database fied col_name
            str1 = str2; //Means Label1.Text=str2 is it possible
        }
    }
 
    return "";
}






This Function is calling on Page_Load of Default.aspx


C#
protected void Page_Load(object sender, EventArgs e)
    {
      //Passing value of p_opt_id
      Main.GetColum_Des("001001001")
    }





Expecting Your Help

[edit] edited for more readability[/edit]
Posted
Updated 4-Aug-10 0:23am
v2
Comments
koool.kabeer 4-Aug-10 9:49am    
are your Labels defined in AspPage?...
or you want to create new Labels?...

I don't know the use of unsafe here in your code. anyway this will help you

C#
public static string GetColum_Des(string p_opt_id,Label label1,Label label2)
   {
       //do your DB opertation
       //this value from your code
       label1.Text = "str";
       //this value from your code
       label2.Text = "str2";
       return "";
   }




C#
protected void Page_Load(object sender, EventArgs e)
   {
       //labels in your page.
       GetColum_Des("your p_opt_id", Label1, Label2);
   }
 
Share this answer
 
Comments
raju melveetilpurayil 4-Aug-10 6:55am    
where you need to show this Labels? you need a PlaceHolder to display this labels.
saifulhaque 4-Aug-10 6:56am    
I think you are not understant my questain ,I want to pass only value of

p_opt_id . Label1,Label2 are generated from database field col_id through

while loop,Then appened .Text with Labels,This will create

Label1.Text,Label2.Text etc.. .After that i want to read value from database

field col_name to Label1.text,Label2.Text etc in while loop ,You can see in my code which had given.
may be this will help you..
i am considering that you have a Database table named as
"Columns" whose data and structure is

col_id ----- col_name ---- opt_ID
______ _________ ________

lbl1 --------- Asp ---------- 1
lbl2 ------ --- C# ---------- 2
lbl3 --------- VB ---------- 22
lbl4 --------- Ado --------- 13

and as you said you have the Labels in on you web Page...
suppose you have assigned that Labels in a Container Control say Panel as
'panel1' as id....
and you have set your Labels ids as in the Database table
"Columns" column "col_id"...

now here is the Code..
/*for getting the Collection of Label Controls from your Container Control say Panel whose id is "panel1" into Collection "labels"*/
var labels = from Control c in panel1.Controls where c.GetType().Equals(typeof(Label)) select c;

 con.Open();
 cmd = new SqlCommand("select col_id,col_name from Columns where opt_ID=@p_opt_id ", con);
 cmd.Parameters.AddWithValue("@p_opt_id", p_opt_id);
  rd = cmd.ExecuteReader();
 while (rd.Read())
 {
    foreach(Control c in labels)/*looping only the Labels
                                Collection of "panel1"*/
    {

       Label l = (Label)c;
       if(l!=null)
       {
           if(l.id==rd["col_id"].ToString())
                l.Text=rd["col_name"].ToString();/*you can set
                                                 various
                                                 Properties of
                                             Label Control here*/
       }
    }
 }

and to set the ID's of your Labels in Your Panel "panel1"..
use the "ID" Property of Label Control that can be set and get....
as... Label1.ID="lbl1";

and i dont know what is "unsafe" in your code....
hope this will help you
may be i am answering quite irrelevant....
if so, sorry friends......
 
Share this answer
 
v5
Comments
saifulhaque 5-Aug-10 0:46am    
I can't understant this statement.Can you clear that. Applying this getting errors var labels = from Control c in panel1.Controls where c.GetType().Equals(typeof(Label)) select c; In my code unsafe i was trying this.Leave that unsafe block.
koool.kabeer 5-Aug-10 4:34am    
from that statement.... you are getting the Label Controls of Panel "panel1" as Collection into variable "labels"...
can you tell me what errors you are getting and which version of your .Net Framework?
koool.kabeer 5-Aug-10 4:35am    
that statement is Language Integrated Query Statement.
koool.kabeer 5-Aug-10 5:00am    
i will give you another example of that statement..
suppose you have an array..
int [] numbers={1,2,3,4,5,6,7,8,9,10};
now
you want to get the Array of Numbers which are divisible by "2".. into another array.. somewhat like this {2,4,6,8,10}
then you can do with this statement..

//code
string s = "";
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var twodivisible = from int i in numbers where i % 2 == 0 select i;
foreach (int i in twodivisible)
{
s = s + "_" + i.ToString();
}
Label1.Text = s;
koool.kabeer 6-Aug-10 8:01am    
what happen....
you didnt get me?
do u have Label Control Label1 on your page. If not create a control



protected void Page_Load(object sender, EventArgs e)
{
Label Label1=new Label();
Main.GetColum_Des("001001001")//Passing value of p_opt_id
}

I think it help u.... :)
 
Share this answer
 
Comments
raju melveetilpurayil 4-Aug-10 6:42am    
Reason for my vote of 1
Label Label1=new Label(); this will not add a control in page.
saifulhaque 4-Aug-10 6:57am    
I had Label in my Default.aspx page I want to pass only value of

p_opt_id . Label1,Label2 are generated from database field col_id through

while loop,Then appened .Text with Labels,This will create

Label1.Text,Label2.Text etc.. .After that i want to read value from database

field col_name to Label1.text,Label2.Text etc in while loop ,You can see in my code which had given.
koool.kabeer 4-Aug-10 9:52am    
can you define.. how to generate the Labels from database?
koool.kabeer 4-Aug-10 9:56am    
i got you in your previous question....
i asked you about where are you adding your labels?,
sorry i just lost for some days...
i think you are asking this...
Label l = new Label();
and you want this 'l' not as 'l' but the string "col_id" of database..
so that you can change Text of Database referenced Label;
saifulhaque 4-Aug-10 10:36am    
I have Label id like Label1,Label2..etc in my Default.aspx.

I want to get automatically label id from database field col_id

(Label1,Label2 ..etc) and appened ".Text" with each Label.Then it will come

Label1.Text,Label.Text2 etc.. .I want read col_name this

Label1.text,Label2.Text etc.. from database field col_name through while

loop.Is it possible through some other way.I am thinking my code is

incorrect.Think another way. Share with your friends also.

Try to Help.

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