views:

1814

answers:

3

I have a sqldatasource with a very simple select statement that should always return one row. I have textboxes on a page that i want to fill with that data from the datasource. how could i do this for textboxes? If there is no data in the database then i want the textboxes to remain empty. How can I accomplish this?

<asp:Panel ID = "Panel2" runat="server" DefaultButton = "save" >
                        <fieldset style="width: 524px"><legend>Rouse InterChange Details</legend>
                        <asp:FormView runat="server" ID="MyFormView" DataSourceID="SqlDataSource3" DefaultMode="Edit">
                        <ItemTemplate >
                        <table>
                            <tr>
                                <td align="right">Interchange ID:</td>
                                <td align="left">
                                    <asp:TextBox ID="txtIntID" runat="server" size="1" MaxLength = "2" Text='<%# Bind("Interchange_Id") %>'></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator8" ValidationGroup = "rouse"  Display ="None" ControlToValidate = "txtIntID" runat="server" ErrorMessage="You Must Provide an Interchange ID."> </asp:RequiredFieldValidator>
                                             <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender11" TargetControlID="RequiredFieldValidator8" HighlightCssClass="validatorCalloutHighlight" runat="server">
                                             </ajaxToolkit:ValidatorCalloutExtender>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">Sender ID:</td>
                                <td align="left">
                                    <asp:TextBox ID="txtsender" runat="server" MaxLength = "15" ></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator9" ValidationGroup = "rouse"  Display ="None" ControlToValidate = "txtsender" runat="server" ErrorMessage="You Must Provide a Sender ID."> </asp:RequiredFieldValidator>
                                     <ajaxToolkit:ValidatorCalloutExtender ID="vce12" TargetControlID="RequiredFieldValidator9" HighlightCssClass="validatorCalloutHighlight" runat="server">
                                     </ajaxToolkit:ValidatorCalloutExtender>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">Interchange Standard ID:</td>
                                <td align="left">
                                    <asp:TextBox ID="ISI" runat="server" size="1" MaxLength = "1" ></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator10" ValidationGroup = "rouse"  Display ="None" ControlToValidate = "ISI" runat="server" ErrorMessage="You Must Provide an Interchange Standard ID."> </asp:RequiredFieldValidator>
                                     <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender12" TargetControlID="RequiredFieldValidator10" HighlightCssClass="validatorCalloutHighlight" runat="server">
                                     </ajaxToolkit:ValidatorCalloutExtender>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">Version:</td>
                                <td align="left">
                                    <asp:TextBox ID="Verstxt" runat="server" size="5" MaxLength = "5" ></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator11" ValidationGroup = "rouse"  Display ="None" ControlToValidate = "Verstxt" runat="server" ErrorMessage="You Must Provide a Version."> </asp:RequiredFieldValidator>
                                     <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender13" TargetControlID="RequiredFieldValidator11" HighlightCssClass="validatorCalloutHighlight" runat="server">
                                     </ajaxToolkit:ValidatorCalloutExtender>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">Functional ID:</td>
                                <td align="left">
                                    <asp:TextBox ID="FuncID" runat="server" size="1" MaxLength = "2" ></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator12" ValidationGroup = "rouse"  Display ="None" ControlToValidate = "FuncID" runat="server" ErrorMessage="You Must Provide a Functional ID."> </asp:RequiredFieldValidator>
                                     <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender14" TargetControlID="RequiredFieldValidator12" HighlightCssClass="validatorCalloutHighlight" runat="server">
                                     </ajaxToolkit:ValidatorCalloutExtender>
                                </td>
                            </tr>
                            <tr style="display:none">
                                <td align="right">Group Control #</td>
                                <td align="left">
                                    <asp:TextBox ID="txtGroupcontrol" runat="server" size="6" MaxLength = "9" ></asp:TextBox>

                                </td>
                            </tr>
                        </table>
                        </ItemTemplate>
                        </asp:FormView>
                        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                        ConnectionString="connectionstring" 
                        SelectCommand="select * from table1 "></asp:SqlDataSource>
                        </fieldset></asp:Panel>
+1  A: 

The easiest way (that I would think to do this) is to write all the data access and form population in the code-behind. I am a little confused, however, that you tagged this as both C# and VB.net. I'll update this post shortly with some C# example code; in the meantime feel free to start comment-spamming with questions. :)

Matt Ball
I can read and write in both languages. And a question gets much more attention when tagged with C#
Eric
I know how to do all of this in code behind by manually connecting and using a data reader, but i would like to do this without that.
Eric
Ah, ok! Then I won't bother with the sample code. In that case, Scott's answer is the way to go.
Matt Ball
Ok. thanks anyways. +1
Eric
+2  A: 

You should consider using a FormView control to do this. Using the binding controls is much easier and cleaner than trying to cram all of your code into the code-behind of the page. I also feel that it makes the page much easier to maintain, since you don't have all of the C#/VB code to manage as well.

<asp:FormView runat="server" ID="MyFormView" DataSourceID="MySqlDataSource" DefaultMode="Edit">
    <EditItemTemplate>
        <table>
            <tr>
                <td align="right">Interchange ID:</td>
                <td align="left">
                    <asp:TextBox ID="txtIntID" runat="server" Text='<%# Bind("InterchangeID") %>' size="1" MaxLength = "2"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator8" ValidationGroup = "rouse"  Display ="None" ControlToValidate = "txtIntID" runat="server" ErrorMessage="You Must Provide an Interchange ID."></asp:RequiredFieldValidator>
                    <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender11" TargetControlID="RequiredFieldValidator8" HighlightCssClass="validatorCalloutHighlight" runat="server">                                             </ajaxToolkit:ValidatorCalloutExtender>
                </td>
            </tr>

        <!-- rest of your table here -->
        </table>
    </EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource runat="server" ID="MySqlDataSource" 
    SelectCommand="SELECT * FROM MyTable" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" >
</asp:SqlDataSource>

See these links for more information:

Scott Ivey
i like where you are going with this but i no longer see my textbox. I edited my code...can you see what i'm doing wrong?
Eric
Never mind scott. My issue was the default mode being set to 'Edit'. Any idea why that is my issue? Don't i want it to be the default?
Eric
Sorry - i wasn't thinking there - you'd want to use the EditItemTemplate instead of ItemTemplate. I'll update my sample...
Scott Ivey
+1  A: 

I like using this in the code behind:

GridView1.DataSource = getQuery(ConnectionString, "select * from mytable");
GridView1.DataBind();

 private DataTable getQuery(string ConnStr, string query)
    {
        DataTable dt = new DataTable();

        try
        {
            using (SqlConnection conn = new SqlConnection(ConnStr))  
            using (SqlDataAdapter cmd = new SqlDataAdapter(query, conn))


                cmd.Fill(dt);
        }
        catch { }
        return dt;
    }

of course not all the details are here

Brad
this i believe is what i was looking for. But i like Scott's idea. Nevertheless +1
Eric