You have to do it in this way:
HTML:
<asp:TextBox ID="txtId" runat="server" />
<asp:TextBox ID="txtName" runat="server" />
<asp:TextBox ID="txtCity" runat="server" />
C#
protected void Page_Load(object sender, EventArgs e)
{
this.GetData();
}
private void GetData()
{
string constr = ConfigurationManager.ConnectionStrings("ConString2").ConnectionString;
using (SqlConnection _cn = new SqlConnection(constr))
{
SqlCommand command = new SqlCommand("SELECT * FROM Person Where Name = @Name", _cn);
// this way you have to add Parameters
command.Parameters.AddWithValue("@Name", "Azim");
_cn.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
// where 0 is column index
txtId.Text = reader.GetValue(0);
txtName.Text = reader.GetValue(1);
txtCity.Text = reader.GetValue(2);
}
}
else
Console.WriteLine("No rows found.");
reader.Close();
}
}
VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.GetData()
End Sub
Private Sub GetData()
Dim constr As String = ConfigurationManager.ConnectionStrings("ConString2").ConnectionString
Using _cn As New SqlConnection(constr)
Dim command As New SqlCommand("SELECT * FROM Person Where Name = @Name", _cn)
'this way you have to add Parameters
command.Parameters.AddWithValue("@Name", "Azim")
_cn.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
If reader.HasRows Then
While reader.Read()
'where 0 is column index
txtId.Text = reader.GetValue(0)
txtName.Text = reader.GetValue(1)
txtCity.Text = reader.GetValue(2)
End While
Else
Console.WriteLine("No rows found.")
End If
reader.Close()
End Using
End Sub
You have to mention index of the column in Reader.GetValue(0)
Thank You.