Problem with DataBinding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suganya
    New Member
    • Dec 2006
    • 39

    Problem with DataBinding

    Hi

    To bind a data from the DB in Labels I have given the coding as

    public partial class SerachResult : System.Web.UI.P age
    {
    SqlConnection con;
    SqlDataReader dr1;
    string str;
    string sql;
    protected void Page_Load(objec t sender, EventArgs e)
    {
    this.Label1 .Text = Request.QuerySt ring["Name"];
    str = "user id=sa;password= cast;database=H ello_Dr;server= AURORA-SERVER";
    con= new SqlConnection(s tr);
    con.Open();
    sql = "SELECT * FROM DoctorRegistrat ion3 where Name=" + Label1.Text + "";
    SqlCommand cmd = new SqlCommand(sql, con);

    dr1 = cmd.ExecuteRead er();
    while(dr1.Read( ))
    {

    Label3.Text = dr1.GetValue(14 ).ToString();
    Label5.Text = dr1.GetValue(12 ).ToString();
    Label7.Text = dr1.GetValue(13 ).ToString();
    Label9.Text = dr1.GetValue(9) .ToString();
    Label11.Text = dr1.GetValue(10 ).ToString();
    Label14.Text = dr1.GetValue(18 ).ToString();
    Label15.Text = dr1.GetValue(19 ).ToString();
    }

    }}


    But when I run the project, it shows the following error

    SqlException was unhandled by user code:
    Incorrect syntax near 'K'.

    The error showing is on the line

    dr1 = cmd.ExecuteRead er();
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Use a try/catch to handle exceptions.
    On your query you probably need to wrap the text in quotes so that it reads
    Code:
    where Name = 'K'
    Something like
    Code:
    "where Name =  '"+text + "'"

    Comment

    • suganya
      New Member
      • Dec 2006
      • 39

      #3
      Originally posted by r035198x
      Use a try/catch to handle exceptions.
      On your query you probably need to wrap the text in quotes so that it reads
      Code:
      where Name = 'K'
      Something like
      Code:
      "where Name =  '"+text + "'"


      Thanks. Its working.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by suganya
        Thanks. Its working.
        Welcome, don't forget to handle potential exceptions using those try/catch blocks though.

        Comment

        Working...