1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Syntax problem on asp.net

Discussion in 'C#' started by alanX, Jul 6, 2006.

  1. #1
    hi all,
    I have a problem on creating the multiple TextBoxs for inserting multiple row into oracle(10g) database.

    Line 41: <% for (int i=0; i<InsertRow;i++) {%>
    Line 42: <tr>

    Line 43: <td><asp:TextBox ID='<% PortNo %>' runat="server"></asp:TextBox></td>

    Line 44: <td><asp:TextBox ID='<% Model %>' runat="server"></asp:TextBox></td>
    Line 45: </tr>



    Parser Error Message: '<% PortNo %>' is not a valid identifier.

    I guess it is a sytax error. can anyone help me. PLZ!
    !
     
    alanX, Jul 6, 2006 IP
  2. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try using <%= PortNo %> and <%= Model %>
     
    vectorgraphx, Jul 6, 2006 IP
  3. alanX

    alanX Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3


    Thanks for your reply,

    I have tried your method. But still fail to create multiple "TestBoxID" using array.

    Line 41: <% for (int i=0; i<InsertRow;i++) {%>
    Line 42: <tr>
    Line 43: <td><asp:TextBox ID='<%=PortNo %>' runat="server"></asp:TextBox></td>
    Line 44: <td><asp:TextBox ID='<%= Model %>' runat="server"></asp:TextBox></td>
    Line 45: </tr>

    Parser Error Message: '<%=PortNo %>' is not a valid identifier.
     
    alanX, Jul 6, 2006 IP
  4. draculus

    draculus Peon

    Messages:
    63
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Make sure that the PortNo and Mode variables are declared at the class level and that they are marked at least protected. If they are private and/or defined as local to a method they will not be available to page.
     
    draculus, Jul 7, 2006 IP
  5. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <asp:textbox id="<%=PortNo%>" runtat="server"> Hello World! </asp:textbox>

    try this hopefully problem will be solved
     
    kashem, Jul 7, 2006 IP
  6. benjymouse

    benjymouse Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No, you cannot - and should not - attempt to generate the ID attribute with a code rendering block/expression.

    I assume that you have som kind of iteration going on here, since thereùs an index i.

    You really should use a repeater for this. Consult the documentation for the asp:Repeater control.
     
    benjymouse, Jul 8, 2006 IP
  7. alanX

    alanX Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hello:
    I still fail to create mutiple "Testbox" ID using array. I have set the array to be global varible.

    So, How to use [asp:Repeater control] to achieve it?

    Thanks
     
    alanX, Jul 18, 2006 IP
  8. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #8
    you can run through a loop with the codebehind

    create a form, or fieldset in the form with an ID and runat="server" then do a loop defining:

    for x as integer = 0 to 10
    dim newTxtBox as TextBox
    newTxtBox.ID = "Box_" & x
    fieldset.controls.add(newTxtBox)
    next

    Something like that...
     
    ccoonen, Jul 18, 2006 IP
  9. alanX

    alanX Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for your reply:

    here is the full code, but i am stuck over this error for a long time. Help me PLZ. error:('<%=PorNo %>' is not a valid identifier)

    <%@ Import Namespace="System.Data.OleDb" %>
    <%@ Import Namespace="System.Data" %>
    <HTML>
    <HEAD>
    <title>----Mutiplie insertion----</title>
    <script language="C#" runat="server">

    int InsertRow;
    string[] Model;
    string[] PortNo;
    string insertCmd;
    public void RowCreated(Object Src, EventArgs E)
    {
    InsertRow = int.Parse(RowNumber.Text);

    for(int i=0;i<InsertRow;i++)
    {
    Model = new string[InsertRow];
    PortNo = new string[InsertRow];
    Model="Model"+i.ToString();
    PortNo="PortNo"+i.ToString();
    }


    }

    private void RowInsert(Object Src, EventArgs E)
    {
    OleDbConnection myConnection = new OleDbConnection("Provider=MSDAORA;Data Source=****;User ID=System;Password=****");

    for(int k=0; k<InsertRow; k++)
    {
    insertCmd = "insert into db2 values('"+PortNo[k].Text+"','"+Model[k].Text+"')";
    OleDbCommand myCommand = new OleDbCommand(insertCmd, myConnection);

    myConnection.Open();
    myCommand.ExecuteNonQuery();

    }

    }
    </script>
    </HEAD>
    <body>
    <form runat="server">
    <table cellpadding="0" cellspacing="0" border="1">
    <tr>
    <td colspan="2">Number of Rows:
    <asp:TextBox ID="RowNumber" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Submmit" OnClick="RowCreated" /></td>
    </tr>
    <tr>
    <td>Model</td>
    <td>PortNo</td>
    </tr>
    <% for (int i=0; i<InsertRow;i++) {%>
    <tr>
    <td><asp:TextBox ID='<%=PorNo %>' runat="server"></asp:TextBox></td>
    <td><asp:TextBox ID='<%=Model %>' runat="server"></asp:TextBox></td>
    </tr>
    <%} %>
    <tr>
    <td><asp:Button ID="Insert" runat="server" Text="Insert" OnClick="RowInsert" /></td>
    </tr>
    </table>
    <asp:Label ID="Message" runat="server"></asp:Label>
    </form>
    </body>
    </HTML>
     
    alanX, Jul 19, 2006 IP
  10. Darrin

    Darrin Peon

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Maybe I'm not reading it right, or not reading your question right, so if I'm way off please ignore.

    It looks like you want to dynamically add textbox controls to your HTML page based on the iteration through the count of the 'i' variable.

    I have to agree with an earlier post that a Repeater Server control might make this a lot easier. Then you can create the template client side and bind your data server side. If you look up examples for the 'Repeater' control I'm sure you'll find quite a bit.

    When I look at the code you have inline with your HTML, I see that you create a for-next loop while i<InsertRow. This looks fine, but if the actual html for the textbox that you are making is not inside the server side code (<% ... %>), then how does it become in the loop? As it is now, it will only show up once. You have to take that HTML that you want in the loop, surround it by your server side code brackets, and then do a Response.Write("<input.....>"); for all of the html that you want dynamically rendered. I think the reason you get the error is because the loop with the i is already done before it gets to the place where you want to use it.

    Does that make sense?

    Let me know if it does not and I can make an example...

    Hope that helps.
     
    Darrin, Jul 19, 2006 IP
  11. alanX

    alanX Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I get your points. It lets me know why i make such error. but i still feel difficulties to complete it. Could you make an example for me.

    Thanks so much.
     
    alanX, Jul 19, 2006 IP
  12. Darrin

    Darrin Peon

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Something along the lines of what I changed in red below:

    <%@ Import Namespace="System.Data.OleDb" %>
    <%@ Import Namespace="System.Data" %>
    <HTML>
    <HEAD>
    <title>----Mutiplie insertion----</title>
    <script language="C#" runat="server">

    int InsertRow;
    string[] Model;
    string[] PortNo;
    string insertCmd;
    public void RowCreated(Object Src, EventArgs E)
    {
    InsertRow = int.Parse(RowNumber.Text);

    for(int i=0;i<InsertRow;i++)
    {
    Model = new string[InsertRow];
    PortNo = new string[InsertRow];
    Model="Model"+i.ToString();
    PortNo="PortNo"+i.ToString();
    }


    }

    private void RowInsert(Object Src, EventArgs E)
    {
    OleDbConnection myConnection = new OleDbConnection("Provider=MSDAORA;Data Source=****;User ID=System;Password=****");

    for(int k=0; k<InsertRow; k++)
    {
    insertCmd = "insert into db2 values('"+PortNo[k].Text+"','"+Model[k].Text+"')";
    OleDbCommand myCommand = new OleDbCommand(insertCmd, myConnection);

    myConnection.Open();
    myCommand.ExecuteNonQuery();

    }

    }
    </script>
    </HEAD>
    <body>
    <form runat="server">
    <table cellpadding="0" cellspacing="0" border="1">
    <tr>
    <td colspan="2">Number of Rows:
    <asp:TextBox ID="RowNumber" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Submmit" OnClick="RowCreated" /></td>
    </tr>
    <tr>
    <td>Model</td>
    <td>PortNo</td>
    </tr>
    <% for (int i=0; i<InsertRow;i++) {
    Response.Write("<tr>");
    Response.Write("<td><input ID='" + PorNo + "'></td>");
    Response.Write("<td><input ID='" + Model + "'></td>");
    Response.Write("</tr>");
    } %>

    <tr>
    <td><asp:Button ID="Insert" runat="server" Text="Insert" OnClick="RowInsert" /></td>
    </tr>
    </table>
    <asp:Label ID="Message" runat="server"></asp:Label>
    </form>
    </body>
    </HTML>
     
    Darrin, Jul 19, 2006 IP
  13. alanX

    alanX Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    thanks for help
    I found there are lots of problems in my original program.
    (not only just assigning the "ID" of "Textboxs", but also the insertion of the such data through the function"private void RowInsert(Object Src, EventArgs E)")

    Could you post the codes which can insert mutiple rows into database(oracle) using asp.net and C#. I really need it and time is not on my side.

    thanks.
     
    alanX, Jul 19, 2006 IP