Click here to Skip to main content
16,014,650 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
protected void Page_Load(object sender, EventArgs e)
    {
       // if (!IsPostBack)
        //{
        //    conn.Open();
        //    SqlDataAdapter da = new SqlDataAdapter("select * from city_master", conn);
        //    DataSet ds = new DataSet();
        //    da.Fill(ds, "city_master");
        //    ddl_city.DataTextField = "city_name";
        //    ddl_city.DataValueField = "city_name";
        //    ddl_city.DataSource = ds;
        //    ddl_city.DataBind();
        //    conn.Close();
        //}
      //  conn.Open();
      //  SqlCommand cmd1 = new SqlCommand("select city_pincode from city_master where city_name='"+ddl_city.SelectedValue+"'", conn);
      //  object pwd;
      //  pwd = cmd1.ExecuteScalar();
      //  txt_pincode.Text = pwd.ToString();
      //  cmd1.ExecuteNonQuery();
      //  conn.Close();

        conn.Open();
        SqlCommand cmd = new SqlCommand("select * from registration where form_no="+int.Parse(Request.QueryString["id"].ToString()),conn);
        SqlDataReader dr;
        dr=cmd.ExecuteReader();

        if (dr.Read())
        {
            txt_datetime.Text = dr["reg_date_time"].ToString();
            txt_fname.Text = dr["first_name"].ToString();
            txt_lname.Text = dr["last_name"].ToString();
            txt_bdate.Text = dr["birth_date"].ToString();
            txt_address.Text = dr["address"].ToString();
          //  ddl_city.SelectedValue = dr["city"].ToString();
            txt_pincode.Text = dr["pincode"].ToString();
            txt_mobile.Text = dr["mobile"].ToString();
            txt_form_no.Text = dr["form_no"].ToString();
            txt_username.Text = dr["user_name"].ToString();
        }
        dr.Close();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
   // protected void ddl_city_SelectedIndexChanged(object sender, EventArgs e)
   // {
     //   conn.Open();
     //   SqlCommand cmd1 = new SqlCommand("select city_pincode from city_master where city_name='" + ddl_city.SelectedValue + "'", conn);
     //   object pwd;
     //   pwd = cmd1.ExecuteScalar();
     //   txt_pincode.Text = pwd.ToString();
     //   cmd1.ExecuteNonQuery();
     //   conn.Close();
   // }
}
Posted
Updated 7-Jun-14 20:15pm
v2
Comments
DamithSL 8-Jun-14 2:36am    
please update your question with full exception details and which line you get this exception etc.
[no name] 8-Jun-14 7:48am    
Because the query string passed to your page does not have parameter "id".

Hi,


I would like to suggest to debug the code by hitting F5. Since you didn't mention in which line you got the error.

Also, please check the datareader object has any rows are not before you start read from it.
C#
if(dr.HasRows())
{
 if(dr.Read())
  {
       // Continue your operation here
  }
}


Hope this helps!

S Francis
My ASPDotNet Blog
 
Share this answer
 
Comments
Member 10858110 8-Jun-14 4:15am    
Respected sir.
if(dr.Hasrows())
{
if(dr.Read())
{
}
}

is not working for me...
and,

SqlCommand cmd = new SqlCommand("select * from registration where form_no="+int.Parse(Request.QueryString["id"].ToString()),conn);

this line got the error...
plzzz can u solve this ???
check below code with few best practice

C#
string sql ="select reg_date_time,first_name,last_name,birth_date,address,pincode,mobile,form_no,user_name from registration where form_no = @fromno";
using(SqlConnection con = new SqlConnection(connectionString))
using(SqlCommand cmd = new SqlCommand(sql),con)
{
    cmd.Parameters.AddWithValue("@fromno", int.Parse(Request.QueryString["id"].ToString()));
    con.Open();
    using(SqlDataReader dr = cmd.ExecuteReader())
    {
        if(dr.HasRows())
        {
            if(dr.Read())
            {
               txt_datetime.Text = dr.GetString(dr.GetOrdinal("reg_date_time"));
               //or txt_datetime.Text = dr.GetString(0);
               //do the same for all other values
            }
        }
    }
}


if you need specific solution you need to give full error details
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900