0

I have 3 tables in my database as "tbl_User", "tbl_City", "tbl_Town".

My "tbl_User" table:

  • userid int [PK],
  • email nvarchar(50),
  • password nvarchar(50),
  • city int,
  • town int

My "tbl_City" table:

  • cityno int [PK],
  • cityname nvarchar(50)

My "tbl_Town" table:

  • townno int,
  • townname nvarchar(50),
  • cityno int

As you can see "tbl_City" and "tbl_Town" tables are related to each other.

While the user is registering on the site, he must choose city and town. So I can save city and town as number in "tbl_User". What I want to do is: When the user goes "profile.aspx", I want the city and town to be seen in the DropDownList selectively and when user click DropDownListCity or DropDownListTown I want all the other cities and towns to appear at the same time. My code bring the city selected in the "tbl_User" and when I click DropDownListCity I can see all other cities. But my code doesn't bring the town selected because "tbl_City" and "tbl_Town" tables are related (when I press the DropDownListCity, DropDownListTown must change accordingly). My code is as follows:

Fonksiyon function = new Fonksiyon();
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GetCity();
        GetTown();
        GetCityAndTownSelectively();
    }
}

private void GetCityAndTownSelectively()
{
    if (Session["userid"] != null)
    {
        DataRow dr = function.GetDataRow("SELECT tbl_City.cityno, tbl_City.cityname, tbl_Town.townno, tbl_Town.townname FROM tbl_User LEFT JOIN tbl_City on tbl_City.cityno = tbl_User.city LEFT JOIN tbl_Town on tbl_Town.townno = tbl_User.town WHERE userid=" + Session["userid"].ToString());
        if (dr == null)
        {
            Response.Redirect("default.aspx");
        }
        else
        {
            DrpDwnLstCity.ClearSelection();
            DrpDwnLstCity.Items.FindByValue(dr[0].ToString()).Selected = true;
            DrpDwnLstTown.ClearSelection();
            DrpDwnLstTown.Items.FindByValue(dr[2].ToString()).Selected = true;
        }
    }
    else
    {
        Response.Redirect("default.aspx");
    }
}

private void GetCity()
{
    SqlConnection conn;
    SqlCommand comm;
    SqlDataReader reader;
    string connectionString = ConfigurationManager.ConnectionStrings["aytasarimConnectionString"].ConnectionString;
    conn = new SqlConnection(connectionString);
    comm = new SqlCommand("SELECT * FROM tbl_City", conn);
    try
    {
        conn.Open();
        reader = comm.ExecuteReader();
        DrpDwnLstCity.DataSource = reader;
        DrpDwnLstCity.DataValueField = "cityno";
        DrpDwnLstCity.DataTextField = "cityname";
        DrpDwnLstCity.DataBind();
        reader.Close();
    }
    catch
    {
        string mesaj = "<script>alert('Error!');</script>";
        Response.Write(mesaj);
    }
}

private void GetTown()
{
    SqlConnection conn;
    SqlCommand comm;
    SqlDataReader reader;
    string connectionString = ConfigurationManager.ConnectionStrings["aytasarimConnectionString"].ConnectionString;
    conn = new SqlConnection(connectionString);
    comm = new SqlCommand("SELECT * FROM tbl_Town WHERE cityno='" + DrpDwnLstCity.SelectedValue + "'", conn);
    try
    {
        conn.Open();
        reader = comm.ExecuteReader();
        DrpDwnLstTown.DataSource = reader;
        DrpDwnLstTown.DataValueField = "townno";
        DrpDwnLstTown.DataTextField = "townname";
        DrpDwnLstTown.DataBind();
        reader.Close();
    }
    catch
    {
        string mesaj = "<script>alert('Error!');</script>";
        Response.Write(mesaj);
    }
}

protected void DrpDwnLstCity_SelectedIndexChanged(object sender, EventArgs e)
{
    GetTown();
}

I get an error: An exception of type 'System.NullReferenceException' occurred in aytasarim.dll but was not handled in user code.

Edit: When I changed my GetTown methods SQL query like this: SELECT * FROM tbl_Town, My code brings town selectively but when I click DrpDwnLstTown I see all towns. The problem is I have to see the town connected to the city.

Shadouspan
  • 253
  • 2
  • 14
  • place a breakpoint on each of these lines 1. `GetCity();` 2. `GetTown();` 3. `GetCityAndTownSelectively();` and see which one throws a `NullReferenceException` – Laazo Sep 15 '17 at 09:39
  • I've try your comment and DrpDwnLstIlce.Items.FindByValue(drbilgi[1].ToString()).Selected = true; this one is throws a NullReferenceException. Probably my problem is the GetTown code because when I changed my SQL query in the GetTown like this: SELECT * FROM tbl_Town. My code brings town selectively but when I click DrpDwnLstTown I see all towns. The problem is I have to see the town connected to the city. – Shadouspan Sep 15 '17 at 09:47

0 Answers0