Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Page_Load(object sender, EventArgs e)
   {
      
       Bindtype();
       

   }


--------------------

private void Bindtype()
{
try
{
string qry = "select MC_Type from dtdc_mctype";
SqlDataAdapter da = new SqlDataAdapter(qry, con);
DataTable dt = new DataTable();


da.Fill(dt);
droptype.DataSource = dt;

droptype.DataValueField = "MC_Type";

droptype.DataBind();
droptype.Items.Insert(0, new ListItem("--Select--", "0"));
}
catch (SqlException ex)
{
ex.Message.ToString();
}


---------------------------

SQL
con.Open();
            qry = "insert into dtdc_ititemmaster (Machine_Sr_no,Model,Type,Make,AMC_Date,Warranty_Date,Dept,User_Name,Identification,IP,Configuration,AMC_Vendor,PO_NO,PO_Date,Remarks) values('" +txtmachineno.Text+ "','" + txtmodel.Text  + "','" + droptype.SelectedItem + "','" + txtmake.Text + "','" + txtamc.Text+ "','" + txtwrrwnty.Text + "','" +dropdept.SelectedItem + "','"+txtuname.Text+"','"+txtidenty.Text+"','"+txtipadd.Text+"','"+txtconfg.Text+"','"+txtamcvndr.Text+"','"+txtpono.Text+"','"+txtpodate.Text+"','"+txtremark.Text+"')";
            SqlCommand cmd = new SqlCommand(qry, con);
            cmd.ExecuteNonQuery();



con.close();
Posted
Comments
Richard Deeming 17-Nov-14 10:04am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

You need to check for IsPostBack in the Page_Load event. If you put some breakpoints you would see what is happening. When you click the button (or whatever you have) to save the value back into the database the Page_Load event runs first and so you rebind the dropdown losing the value that the user selected.

Do this instead in your Page_Load

C#
protected void Page_Load(object sender, EventArgs e)
   {
      // only want to do this the first time the page loads, not during postbacks
      if (!IsPostBack)
      {
         Bindtype();
      }       
   }



Also change your SQL to use parameters so you are not vulnerable to sql injections.
 
Share this answer
 
v2
Comments
Member 11240491 17-Nov-14 9:25am    
Sir,

I tryed as above u said, but the data is not inserting it show empty
ZurdoDev 17-Nov-14 9:31am    
You'll need to debug your code then and find out what's happening. You also need to change your sql to use parameters so that you are not leaving yourself vulnerable to sql injections.
i am using asp:content page it is no working,,

but it is working on under html pages how can is use in asp:content pages
 
Share this answer
 
Hi,

Change your SQL as below:

To get value of selected item from dropdown in asp.net, you need to user dropdown.SelectedItem.value.

SQL
con.Open();
qry = "insert into dtdc_ititemmaster (Machine_Sr_no,Model,Type,Make,AMC_Date,Warranty_Date,Dept,User_Name,Identification,IP,Configuration,AMC_Vendor,PO_NO,PO_Date,Remarks) values('" +txtmachineno.Text+ "','" + txtmodel.Text  + "','" + droptype.SelectedItem.value + "','" + txtmake.Text + "','" + txtamc.Text+ "','" + txtwrrwnty.Text + "','" +dropdept.SelectedItem.value + "','"+txtuname.Text+"','"+txtidenty.Text+"','"+txtipadd.Text+"','"+txtconfg.Text+"','"+txtamcvndr.Text+"','"+txtpono.Text+"','"+txtpodate.Text+"','"+txtremark.Text+"')";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
 
Share this answer
 
Comments
Richard Deeming 17-Nov-14 10:05am    
You've copied the SQL Injection[^] vulnerability from the question.
Dusara Maulik 17-Nov-14 22:58pm    
I gave solution within his existing source.

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