Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
OleDbConnection con;
StringBuilder StrBul;
DataSet ds;
OleDbDataAdapter da;
OleDbCommand cmd;

protected void Page_Load(object sender, EventArgs e)
{
    DataLoad();
}
public void DataLoad()
{
    connection();
    DataTable dt = new DataTable();
    StrBul = new StringBuilder();
    StrBul.AppendFormat("SELECT Country_Id,Country_Name FROM Country_Registration");
    da = new OleDbDataAdapter(StrBul.ToString(), con);
    ds=new DataSet ();
    da.Fill(dt);
    ddlcountry.DataSource = dt;
    ddlcountry.DataTextField = "Country_Name";
    ddlcountry.DataValueField = "Country_Id";
    ddlcountry.DataBind();
}
public void connection()
{
    string StrConnection = ConfigurationManager.ConnectionStrings["EGPApplication"].ConnectionString;
    con = new OleDbConnection(StrConnection);
    if (con.State == ConnectionState.Open)
        con.Close();
    else
        con.Open();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{

        connection();
        string a = ddlcountry.SelectedValue;

        StrBul = new StringBuilder();
        StrBul.AppendFormat("Select max(SERVICECHARGE_ID) from Servicecharge_Details");
        da = new OleDbDataAdapter(StrBul.ToString(), con);
        ds = new DataSet();
        da.Fill(ds);
        Int32 int32Id = 0;
        if (ds.Tables[0].Rows[0].ItemArray[0].ToString() == "")
        {
            int32Id = 1;
        }
        else
        {
            int32Id = Convert.ToInt32(ds.Tables[0].Rows[0].ItemArray[0].ToString()) + 1;
        }
        StrBul = new StringBuilder();
        StrBul.AppendFormat("Insert into Servicecharge_Details Values({0},'{1}',{2},{3},{4})", int32Id, txtbankname .Text, txtservicecharge .Text ,ddlcountry.SelectedValue   ,1 );
        cmd = new OleDbCommand(StrBul.ToString(), con);
        cmd.ExecuteNonQuery();
        ClientScript.RegisterClientScriptBlock(GetType(), "aa", "alert('Data inserted successfully')", true);
        Clear();

    }




Im using this code for get country_id in dropdownlist from database. but while im inserting that value to another table the country_id should be same in all insert statement.example
if i select india as country the id is 1
if i select USA that one also getting the same country_id 1...but actual value is 2.

how can i solve this pblm.

is any other solution for that?plz help me to correct that error


Thanks
Posted

Change your code to this.


C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               DataLoad();
           }
       }
 
Share this answer
 
in page load add IsPostback[^]

C#
protected void Page_Load(object sender, EventArgs e)
    {
      if(!IsPostback)
         DataLoad();
    }
 
Share this answer
 
v2
Comments
121ss 9-Jan-14 4:57am    
thanku. now im getting
Bojjaiah 9-Jan-14 4:57am    
welcome. :)
when you click button the page post back again and your dropdownlist bind again go to the first index and that is why it is getting same value every time.

just add

is(!ispostback)
{
binddropdown();
}


it should work.
 
Share this answer
 

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