Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In run mode as follows

Name textbox1

Designation Courseofficer (Dropdownlist)
Prinicpal
Director


Save(Buuton)

in the designation column three values are there in the dropdownlist


When user type the name ram in textbox1 and select the designation from the dropdownlist.

Suppose i am selecting the Courseofficer from the dropdownlist and click Save button records are saved.


but i am selecting the courseofficer from the dropdownlist only. But in database
last value Director is saved in the database.


Save button code as follows

Sql = "insert into Designations values('" + txt_name.Text + "','" + ddldesg.SelectedValue.ToString() + "')";
SCon.ExecSql(Sql);
LblErr.Text = "Record Inserted Successfully";
SCon.Con.Close();

In database records as follows

ID Name Designations
1 Ram Director

please help me what is the problem in my above code.
Posted

There are a lot of possibilities why the wrong value could be saved. For example if the values do not correspond the texts in the drop down list. Based on your example it actually looks like you're not using the value at all, only text...

To ensure that the values are correct place a breakpoint on the line where you get the value from the drop down list and using the debugger investigate the selectedvalue and the selecteditem properties. Do the values seem as they should.

Another thing is that you should not concatenate the values from the textboxes etc directly to the SQL statements. This leaves you open to SQL injections and often introduces conversion problems. Instead you should always use parameters, like SqlParameter[^] to provide the values to the statements.
 
Share this answer
 
C#
Sql = "insert into Designations values('" + txt_name.Text + "','" + ddldesg.SelectedValue.ToString() + "')";

The above One u wrote derive the values only like
1=principal
2=course officer
3=director Items if Select principal it store one
try this
This following answer is,as per ur database,
C#
Sql = "insert into Designations values('" + txt_name.Text + "','" + ddldesg.SelectedItem.Text + "')";
 
Share this answer
 
v3
Comments
Arasappan 12-Aug-15 3:03am    
Show your dropdownlist function

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