The problem is your Page_Load
. Change it to:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataSource df = new SqlDataSource();
df.ConnectionString = conn;
df.SelectCommand = "SELECT [ID], [Kategorije] FROM [kategorije] ";
kategorije.DataSource = df;
kategorije.DataTextField = "Kategorije";
kategorije.DataValueField = "ID";
kategorije.DataBind();
}
}
The problem is your rebinding kategorije
every time your code does a postback so by the time Button1_Click
runs, kategorije
has been rebinding and reset so the SelectedValue
is null.
When a postback happens your Page_Load
code runs first, then the Button1_Click
runs right after. Moving that code into a !IsPostBack
check will cause it to only run the first time your page loads and not again after so the SelectedValue
will be available.
Side note: You should also improve your accepted answer rate... you have asked a bunch of question and not marked any as the accepted answer. If you improve your accept rate (which is 0%) and people will be more likely to help you out. Please review the FAQ and specifically the section: How do I ask questions here?