Hi micah,
Refer the below sample code.
HTML
<div>
Name:<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
Country<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
txtName.Text = GetData("SELECT TOP 1 Name FROM Customers", CommandType.Text);
txtCountry.Text = GetData("SELECT TOP 1 Country FROM Customers", CommandType.Text);
}
public string GetData(string query, CommandType commandType)
{
string str = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType = commandType;
con.Open();
object i = cmd.ExecuteScalar();
return i != null ? i.ToString() : string.Empty;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
txtName.Text = GetData("SELECT TOP 1 Name FROM Customers", CommandType.Text)
txtCountry.Text = GetData("SELECT TOP 1 Country FROM Customers", CommandType.Text)
End Sub
Public Function GetData(query As String, commandType As CommandType) As String
Dim str As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim dt As New DataTable()
Dim con As New SqlConnection(str)
Dim cmd As New SqlCommand(query, con)
cmd.CommandType = commandType
con.Open()
Dim i As Object = cmd.ExecuteScalar()
Return If(i IsNot Nothing, i.ToString(), String.Empty)
End Function
Screenshot