Dropdownlistbox issue....
-
Hi guys, Actually im developing a web application on VS2003 framework 1.1, in this application i have several textboxes and one dropdown list box. Now the items in my dropdown listbox gets populated from the database table also i have an insert button on clicking it the datas in the textbox and also the selected item in the dropdown list box gets inserted into my database table. Here comes the issue now in my dropdown listbox say there are 2 items for example --> Tiger, Lion... since here tiger is the first element by default my listbox shows tiger may be from the dropdown list if i select lion it inserts only tiger.. in short my listbox doesnt identify my selected item.. The code is as follows:
private void Button1_Click(object sender, System.EventArgs e)
{
try
{
card = DropDownList1.SelectedItem.Text.ToString();
name = TextBox34.Text;
age = TextBox35.Text;
gender = TextBox36.Text;
con = new SqlConnection();
con.ConnectionString = Session["sqlcon"].ToString();
con.Open();
cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into card_details values('"+Session["User_ID"]+"',getDate(),'"+name+"','"+age+"','"+gender+"','"+card+"')";
cmd.Connection = con;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
catch (Exception exp)
{
Label39.Visible = true;
Label39.Text = "Exception occured review log file for details";
ExceptionHandler.HandleException("Exception",exp);
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (con != null)
{
con.Close();
con.Dispose();
}
}
}I really dont know whats wrong in my code do plz help me.. Thanks & Regards, Tash
-
Hi guys, Actually im developing a web application on VS2003 framework 1.1, in this application i have several textboxes and one dropdown list box. Now the items in my dropdown listbox gets populated from the database table also i have an insert button on clicking it the datas in the textbox and also the selected item in the dropdown list box gets inserted into my database table. Here comes the issue now in my dropdown listbox say there are 2 items for example --> Tiger, Lion... since here tiger is the first element by default my listbox shows tiger may be from the dropdown list if i select lion it inserts only tiger.. in short my listbox doesnt identify my selected item.. The code is as follows:
private void Button1_Click(object sender, System.EventArgs e)
{
try
{
card = DropDownList1.SelectedItem.Text.ToString();
name = TextBox34.Text;
age = TextBox35.Text;
gender = TextBox36.Text;
con = new SqlConnection();
con.ConnectionString = Session["sqlcon"].ToString();
con.Open();
cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into card_details values('"+Session["User_ID"]+"',getDate(),'"+name+"','"+age+"','"+gender+"','"+card+"')";
cmd.Connection = con;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
catch (Exception exp)
{
Label39.Visible = true;
Label39.Text = "Exception occured review log file for details";
ExceptionHandler.HandleException("Exception",exp);
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (con != null)
{
con.Close();
con.Dispose();
}
}
}I really dont know whats wrong in my code do plz help me.. Thanks & Regards, Tash
<> wrote:
whats wrong in my code
Plenty.
name = TextBox34.Text;
First of all you are accepting direct, unvalidated input from user controls that can and will expose your application to SQL inject attacks.con.ConnectionString = Session["sqlcon"].ToString();
Do not store the connection string in a Session variable. Another security hole and unnecessary. You don't even handle the case where it could be null.card = DropDownList1.SelectedItem.Text.ToString();
When the button is clicked, whatever the currently selected item in the dropdown is what will be used.
I know the language. I've read a book. - _Madmatt
-
<> wrote:
whats wrong in my code
Plenty.
name = TextBox34.Text;
First of all you are accepting direct, unvalidated input from user controls that can and will expose your application to SQL inject attacks.con.ConnectionString = Session["sqlcon"].ToString();
Do not store the connection string in a Session variable. Another security hole and unnecessary. You don't even handle the case where it could be null.card = DropDownList1.SelectedItem.Text.ToString();
When the button is clicked, whatever the currently selected item in the dropdown is what will be used.
I know the language. I've read a book. - _Madmatt
Mark Nischalke wrote:
When the button is clicked, whatever the currently selected item in the dropdown is what will be used.
what ever item i select from the list box only the 1st element in the listbox gets inserted... may be if i select 5th item in the list and click the insert button the first element in the list gets inserted... why does that happen.. im confused...
-
Hi guys, Actually im developing a web application on VS2003 framework 1.1, in this application i have several textboxes and one dropdown list box. Now the items in my dropdown listbox gets populated from the database table also i have an insert button on clicking it the datas in the textbox and also the selected item in the dropdown list box gets inserted into my database table. Here comes the issue now in my dropdown listbox say there are 2 items for example --> Tiger, Lion... since here tiger is the first element by default my listbox shows tiger may be from the dropdown list if i select lion it inserts only tiger.. in short my listbox doesnt identify my selected item.. The code is as follows:
private void Button1_Click(object sender, System.EventArgs e)
{
try
{
card = DropDownList1.SelectedItem.Text.ToString();
name = TextBox34.Text;
age = TextBox35.Text;
gender = TextBox36.Text;
con = new SqlConnection();
con.ConnectionString = Session["sqlcon"].ToString();
con.Open();
cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into card_details values('"+Session["User_ID"]+"',getDate(),'"+name+"','"+age+"','"+gender+"','"+card+"')";
cmd.Connection = con;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
catch (Exception exp)
{
Label39.Visible = true;
Label39.Text = "Exception occured review log file for details";
ExceptionHandler.HandleException("Exception",exp);
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (con != null)
{
con.Close();
con.Dispose();
}
}
}I really dont know whats wrong in my code do plz help me.. Thanks & Regards, Tash
It sounds like you are populating the listbox but not taking PostBack into account. Wherever it is you are binding the data surround the code with:
if (!PostBack)
{
... code to bind listbox to data.
}otherwise every time you hit the button the listbox gets repopulated and you will always get the first item in the list as your value.
Tychotics: take us back to the moon "Life, for ever dying to be born afresh, for ever young and eager, will presently stand upon this earth as upon a footstool, and stretch out its realm amidst the stars." H. G. Wells
-
Mark Nischalke wrote:
When the button is clicked, whatever the currently selected item in the dropdown is what will be used.
what ever item i select from the list box only the 1st element in the listbox gets inserted... may be if i select 5th item in the list and click the insert button the first element in the list gets inserted... why does that happen.. im confused...
Is AutoPostBack property set to true?
Naina
-
It sounds like you are populating the listbox but not taking PostBack into account. Wherever it is you are binding the data surround the code with:
if (!PostBack)
{
... code to bind listbox to data.
}otherwise every time you hit the button the listbox gets repopulated and you will always get the first item in the list as your value.
Tychotics: take us back to the moon "Life, for ever dying to be born afresh, for ever young and eager, will presently stand upon this earth as upon a footstool, and stretch out its realm amidst the stars." H. G. Wells
-
Is AutoPostBack property set to true?
Naina
-
Thanx alot u solved my probs... but let me knw y we use
digital man wrote:
if (!PostBack) { ... code to bind listbox to data. }
thanx and regards, Tash
Simply put, this construct tells the page that it should only do whatever is inside the 'if' should this be the first time the page has been loaded.
Tychotics: take us back to the moon "Life, for ever dying to be born afresh, for ever young and eager, will presently stand upon this earth as upon a footstool, and stretch out its realm amidst the stars." H. G. Wells