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>   

4 Answers 4

3

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:

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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?
Sorry - i wasn't thinking there - you'd want to use the EditItemTemplate instead of ItemTemplate. I'll update my sample...
1

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. :)

3 Comments

I can read and write in both languages. And a question gets much more attention when tagged with C#
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.
Ah, ok! Then I won't bother with the sample code. In that case, Scott's answer is the way to go.
1

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

1 Comment

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

I do this all the time in C# as opposed to "default" loading data from the controls themselves. That way I know that when my webpage loads, I control the data load, not ASP! My method below is also great in avoiding generating connectors, datasets, and other controls just to get one piece of data from one row, from one table for example.

                SqlDataSource sdsClogdetails = (SqlDataSource)gvRow.FindControl("sdsCLdetails");
                if (sdsClogdetails != null)
                {
                    DataView dv = sdsClogdetails.Select(DataSourceSelectArguments.Empty) as DataView;
                    if (dv != null)
                    {
                        DataTable dt = dv.ToTable() as DataTable;
                        if (dt != null)
                        {
                            DataRow dr = (DataRow)dt.Rows[0];
                            txtCLOG.Text = dr.ItemArray[3].ToString();
                        }
                    }
                }

So in my example, the ASP control I want to place data into is called txtCLOG, which is a TextBox control. Obviously the data is inside the dataset 3 cells along. So if your datasource returns 10 columns of data, then my example would bring in the 3rd column's data into the textbox control.

Using the above code, or similar, I get to hit many birds with one stone and I don't have to manage disposing of controls held in memory either. Of course a better way to have done the above is to use the USING statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.