Edd Davis
Edd Davis

Reputation: 59

ASP.net listbox - selection index always -1 even with !ispostback

I'm really struggling, i've been working on this all day,

i have a listbox populated by a SQL database.

I have set the binding of the listbox in a if(!this.ispostback) block

I have a button that eventually will run a query and update the datebase, based on what is selected in the listbox.

Every time that button is clicked, the selection is lost and therefore there is a Null Exemption when trying to get the value of the selected item in the listbox.

here is the code - please can you help

C#

protected void Page_Load (object sender, EventArgs e)
    {
        if(!this.IsPostBack)
        {
            bind();
        }
    }

private void bind()
    {
        DataSet ds = new DataSet();
        string constring = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        string queryShrewsbury = "Select Callsign, Info FROM Trucks WHERE Location Like'Shrewsbury'";
        //string queryDonnington = "Select Callsign, Info FROM Trucks WHERE Location Like'Donnington'";
        SqlConnection conn1 = new SqlConnection(constring);
        conn1.Open();
        SqlDataAdapter adp = new SqlDataAdapter(queryShrewsbury, constring);
        adp.Fill(ds);
        Shrewsbury_listbox.DataSource = ds;
        Shrewsbury_listbox.DataTextField = "Callsign";
        Shrewsbury_listbox.DataValueField = "Info";
        Shrewsbury_listbox.DataBind();
        conn1.Close();
}


protected void Button1_Click(object sender, EventArgs e)
    {
        if (Shrewsbury_listbox.SelectedIndex == -1)
        {
            Response.Write("it didn't work");
        }
        else
        {
            string value = Shrewsbury_listbox.SelectedItem.Value;
            Response.Write(value + "it worked");
        }
    }

HTML

<

body>

    <form id="form1" runat="server" enableviewstate="true">

    <asp:ListBox ID="Shrewsbury_listbox" runat="server" Height="153px" Width="225px" EnableViewState="true" ></asp:ListBox>

    <br />
    <asp:Button ID="Button1" runat="server" Text=">>" OnClick="Button1_Click" Height="53px" Width="221px" />

</form>

</body>

Onedrive link to application

Upvotes: 2

Views: 175

Answers (3)

Edd Davis
Edd Davis

Reputation: 59

I finally Managed to work out what I had done.

I thought it had something to do with the conenction string, but still seemed to replicate the problem when trying different strings.

So then I wondered why it was working on other peoples computers when they were putting in test data. So I started up a new project and did a test database with a few values, and it worked fine.

So i thougth I had solved it, I then copied the data back from an excel spreadsheet into the database, however when i did that, the ID number increased, not starting from 1 (as i had already added and deleted some records)

Therefore it was causing a validation error. not sure of the technical info behind it, but if anyone has an idea on why a different ID primary key, not starting at 1 could be problem please let me know.

Anyway to fix this problem, i moved the primary key to the end of the table, created a new table with the primary key starting at 1, and then copied the data back from excel with VS making a new primary key starting from 1. seems to have solved the problem now.

Thank you to everyone who had a look at it.

Upvotes: 1

Bukhari
Bukhari

Reputation: 502

Work

I've copy whole your code on Fleet_Tracker_Second.aspx, Fleet_Tracker_Second.aspx.cs, and Fleet_Tracker_Second.aspx.designer.cs, but i change the query string and query and still works.

Upvotes: 0

Wade Womersley
Wade Womersley

Reputation: 1

Doesn't the form require runat="server" in it for postback to work correctly?

The form has "runat=server" and still doesn't work correctly

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server" enableviewstate="true">
<title></title>
<style type="text/css">
    #form1 {
        height: 249px;
    }
</style>
</head>
<body>
<form id="form1" runat="server" enableviewstate="true">

    <asp:ListBox ID="Shrewsbury_listbox" runat="server" Height="153px" Width="225px" EnableViewState="true" ></asp:ListBox>

    <br />
    <asp:Button ID="Button1" runat="server" Text=">>" OnClick="Button1_Click" Height="53px" Width="221px" />

</form>

Upvotes: 0

Related Questions