counter ב- ASPNET

amera

New member
איך מטפלים בשגיאה הבאה:

Compiler Error Message: BC30616: Variable 'fields' hides a variable in an enclosing block. Source Error: Line 45: Line 46: Dim k As Short Line 47: Dim fields Line 48: for k=0 to dr_users.FieldCount - 1 Line 49: if dr_users.GetName(k)<>"user_fname" and dr_users.GetName(k)<>"user_mname" and dr_users.GetName(k)<>"user_lname" then Source File: d:\inetpub\wwwroot\school_reports\print_pupils.aspx Line: 47​
 

gilad g

New member
יש לך משתנה fields כפול

כלומר, במקום אחר באותה מתודה הצהרת על משתנה fields, ועכשיו אתה מצהיר עליו שוב
 

amera

New member
נכון, שיניתי אחד המשתנים לשם אחר

ולא עבד, להלן הקוד
sub Page_Load if Not Page.IsPostBack Then Session.Timeout = 60 Dim str As String if session("educator") = "ok" then 'used for the page educator_menu.asp when the educator wants a list of his pupils str = session("class_id") Dim strArray() as String = split(str,";") str="11e" 'strArray(0) 'the classes an educator educates else str = "11e,12e,10e" 'session("class_id") 'All classes in a school. This page is accessed by the school secretary end if str = "(" & str & ")" str=replace(str," ", "") str=replace(str,",", "','") str=replace(str,"(", "'") str=replace(str,")", "'") Dim strConn As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("...\data\school_reports.mdb") Dim conn As OleDbconnection=New OleDbconnection(strConn) conn.Open() Dim sql_classes As String = "select * from classes where class_id in (" & str & ")" Dim sql_users As String = "select * from users" Dim cmd_classes As OleDbCommand=New OleDbCommand(sql_classes,conn) Dim cmd_users As OleDbCommand=New OleDbCommand(sql_users,conn) Dim dr_classes, dr_users As OleDbDataReader dr_classes=cmd_classes.ExecuteReader() RepClasses.DataSource=dr_classes RepClasses.DataBind() dr_classes.Close() dr_users=cmd_users.ExecuteReader() Dim k As Short Dim fields for k=0 to dr_users.FieldCount - 1 if dr_users.GetName(k)<>"user_fname" and dr_users.GetName(k)<>"user_mname" and dr_users.GetName(k)<>"user_lname" then 'the complete name of the pupils will always be represented and such there is no need to ask the secretary if he likes to display them fields.Text=dr_users.GetName(k) end if next dr_users.Close() conn.Close() End If Dim school_id As String = "800086" 'session("school_id") Dim classes As String = "11e" 'request("classes") ' class(es) that the educator chose to display classes="(" & classes & ")" classes=replace(classes," ", "") classes=replace(classes,",", &quo​
 

amera

New member
נכון, שיניתי אחד המשתנים לשם אחר

ולא עבד, להלן הקוד
sub Page_Load if Not Page.IsPostBack Then Session.Timeout = 60 Dim str As String if session("educator") = "ok" then 'used for the page educator_menu.asp when the educator wants a list of his pupils str = session("class_id") Dim strArray() as String = split(str,";") str="11e" 'strArray(0) 'the classes an educator educates else str = "11e,12e,10e" 'session("class_id") 'All classes in a school. This page is accessed by the school secretary end if str = "(" & str & ")" str=replace(str," ", "") str=replace(str,",", "','") str=replace(str,"(", "'") str=replace(str,")", "'") Dim strConn As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("...\data\school_reports.mdb") Dim conn As OleDbconnection=New OleDbconnection(strConn) conn.Open() Dim sql_classes As String = "select * from classes where class_id in (" & str & ")" Dim sql_users As String = "select * from users" Dim cmd_classes As OleDbCommand=New OleDbCommand(sql_classes,conn) Dim cmd_users As OleDbCommand=New OleDbCommand(sql_users,conn) Dim dr_classes, dr_users As OleDbDataReader dr_classes=cmd_classes.ExecuteReader() RepClasses.DataSource=dr_classes RepClasses.DataBind() dr_classes.Close() dr_users=cmd_users.ExecuteReader() Dim k As Short Dim fields for k=0 to dr_users.FieldCount - 1 if dr_users.GetName(k)<>"user_fname" and dr_users.GetName(k)<>"user_mname" and dr_users.GetName(k)<>"user_lname" then 'the complete name of the pupils will always be represented and such there is no need to ask the secretary if he likes to display them fields.Text=dr_users.GetName(k) end if next dr_users.Close() conn.Close() End If Dim school_id As String = "800086" 'session("school_id") Dim classes As String = "11e" 'request("classes") ' class(es) that the educator chose to display classes="(" & classes & ")" classes=replace(classes," ", "") classes=replace(classes,",", &quo​
 

amera

New member
ההמשך

Dim classes As String = "11e" 'request("classes") ' class(es) that the educator chose to display classes="(" & classes & ")" classes=replace(classes," ", "") classes=replace(classes,",", "','") classes=replace(classes,"(", "('") classes=replace(classes,")", "')") 'response.write classes &"<br>" Dim fields = "user_id" 'request("fields") fields=replace(fields," ", "") fields=split(fields,",") End Sub​
 

gilad g

New member
תקן את השורה כך: ../images/Emo13.gif

fields = "user_id" 'request("fields")​
תוריד את המילה Dim
 

amera

New member
קיבלתי את ה- ERROR הבא:

Compiler Error Message: BC30311: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.CheckBox'. Source Error: Line 65: classes=replace(classes,")", "')") Line 66: 'response.write classes &"<br>" Line 67: fields = "user_id" 'request("fields") Line 68: fields=replace(fields," ", "") Line 69: 'response.write fields &"<br>" Source File: d:\inetpub\wwwroot\school_reports\print_pupils.aspx Line: 67​
 

gilad g

New member
כתוב לך בדיוק מהי הבעיה ../images/Emo13.gif

fields הוא checkbox, אז אתה לא יכול להכניס אליו string....
 

amera

New member
ניסיתי להגדיר שם חדש fields2

וזה לא פתר את הבעיה
 

amera

New member
במקום

DIM fields = "user_id"​
עשיתי
DIM fields2 = "user_id"​
ושיניתי למטה מ- fields ל- fields2
 

amera

New member
להלן

Exception Details: System.NullReferenceException: Object variable or With block variable not set. Source Error: Line 49: if dr_users.GetName(k)<>"user_fname" and dr_users.GetName(k)<>"user_mname" and dr_users.GetName(k)<>"user_lname" then Line 50: 'the complete name of the pupils will always be represented and such there is no need to ask the secretary if he likes to display them Line 51: fields.Text=dr_users.GetName(k) Line 52: end if Line 53: next Source File: d:\inetpub\wwwroot\school_reports\print_pupils.aspx Line: 51​
יש לי את השורה הבאה בתחתית הדף:
<asp:checkbox runat="server" id="fields" text='dddd'></asp:checkbox>​
 

gilad g

New member
באמת מוזר

זאת ממש יריה באפלה, אבל תנסה לשנות את הגרשים למרכאות... כלומר במקום:
<asp:checkbox runat="server" id="fields" text='dddd'></asp:checkbox>​
תרשום:
<asp:checkbox runat="server" id="fields" text="dddd"></asp:checkbox>​
 

nattygur

New member
מספר הערות

1)הבעיה היא שהגדרתה משתנה מסוג (object) והצבעתה אותו למשתנה מסוג webControl. לאחר מכן ניסיתה להגדיר אותו שנית ולכן קבלתה את ה Error הראשון. כאשר הורדתה את ה Dim ניסיתה להכניס משתנה מסוג String למשתנה מסוג WebControl ולכן קבלתה Error על Casting. עכשיו הגדרתה משתנה חדש וה Error הנוכחי יכול להיות בגלל בעיה ב DataReader ולא ב TextBox. 2) לא ההיתה נופל לבור הזה אם הייתה מקפיד להגדיר את סוגי המשתנים (Dim Fields as TextBox or as WebControl). זה היה פותר לך בעיות וגם היה עוזר לאחרים להבין יותר טוב את הקוד שלך. 3) אתה משתמש הרבה פעמים ב Replace של String. זוהיא פעולה בזבזנית בשל טיבעו של String ב Net.. משתנים מסוג String הם Imutable הווה אומר הם לא משתנים כל שינוי ב String גורר יצירת String אחר. ישנה מחלקה בשם StringBuilder שהשימוש בה לשרשור Strings ו Replace עדיף (ראה Text NameSpace ). 4) עדיף לאכסן ConnectString לא בתוך הקוד. ניתן לעשות זאת בקלות רבה ב Web.Config. 5) ככלל עדיף לבנות מערכות Web לפי מודל השכבות. מודל זה מפריד בין שכבת התצוגה, לוגיקה עסקית וגישה לבסיס נתונים. כל שכבה צריכה להיות ב assembly נפרד. מודל זה מאפשר טיפול ותחזוקה יותר נוחים של אפליקציה.תחשוב בכמה מסכים תצתרך לבקר בכדי לטפל בשינוי שנעשה בבסיס הנתונים נתי גור [MVP]
 

amera

New member
counter ב- ASPNET

אני מתכוון לבנות COUNTER שמבוסס על מסד נתונים. שהא ל- ASPNET יש משהו טוב וקצר לבניית COUNTER
 
למעלה