Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 1 dropdownlist and i m binding thatby coding and on its datatextfield property i m binding by company name and DataValueField by company id.and at the time of insertion i m using the dropdown's selected value to insert the company code in database and its datatype is int so i m converting that in integer but its not doing work giving exception at runtime that input string is not in correct format..

Code is as follows:=
C#
try
                {
                    SqlDataAdapter adap = new SqlDataAdapter("select br_id,br_name,cmp_id from branch where br_code='" + Session["relative_dept"].ToString() + "'", con);
                    DataTable dt1 = new DataTable();
                    adap.Fill(dt1);
                    if(dt1.Rows.Count > 0)
                    {
                     for(int i = 0; i < dt1.Rows.Count; i++)
                     {
                    str = dt1.Rows[0]["cmp_id"].ToString();
                    ddl_brid.DataSource = dt1;
                    ddl_brid.DataTextField = dt1.Rows[i]["br_name"].ToString();
                    ddl_brid.DataValueField = dt1.Rows[i]["br_id"].ToString();
                    ddl_brid.Items.Add(dt1.Rows[i]["br_name"].ToString());
                     }
                    }
                    
                }
                catch { }

and at insertion button>
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString);
        if (con.State == ConnectionState.Closed)
            con.Open();
        try
        {
            SqlCommand cmd = new SqlCommand("insert into department(cmp_id,br_id,dept_name,dept_description) values(@cmp_id,@br_id,@dept_name,@dept_description)", con);
            cmd.Parameters.AddWithValue("@cmp_id", Convert.ToInt32(ddl_company.SelectedValue));
            cmd.Parameters.AddWithValue("@br_id",Convert.ToInt32(ddl_brid.SelectedValue));            
            cmd.Parameters.AddWithValue("@dept_name",txt_deptname.Text);
            cmd.Parameters.AddWithValue("@dept_description",txt_description.Text);
            cmd.ExecuteNonQuery();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Success", "<script>alert('Department Created');window.location.href = 'logindetails.aspx'; </script>", false);
            Session["dept"] = "DA";
        }
        catch(Exception ex)
        {
            ScriptManager.RegisterStartupScript(this,this.GetType(), "Failed", "alert('Department Creation Failed.')",true);
        }
Posted
Updated 17-Oct-11 3:04am
v2
Comments
uspatel 17-Oct-11 9:04am    
pre tag added
I.explore.code 17-Oct-11 9:24am    
ok, this is not directly related to your question and hence I am adding this as a comment. I would recommned turning the Select query into a parametrised query too just like you have done for the insert statement just a few lines down...

Cheers.

Hi,

you've to change some code

C#
if(dt1.Rows.Count > 0)
                  {
                   for(int i = 0; i < dt1.Rows.Count; i++)
                   {

                  ddl_brid.Items.Add(new ListItem(dt1.Rows[i]["br_name"].ToString(), dt1.Rows[i]["br_id"].ToString()));
                   }
         }


All the Best
 
Share this answer
 
C#
if(dt1.Rows.Count > 0)
                   {
                    for(int i = 0; i < dt1.Rows.Count; i++)
                    {
                   str = dt1.Rows[0]["cmp_id"].ToString();
                   ddl_brid.DataSource = dt1;
                   ddl_brid.DataTextField = dt1.Rows[i]["br_name"].ToString();
                   ddl_brid.DataValueField = dt1.Rows[i]["br_id"].ToString();
                   ddl_brid.Items.Add(dt1.Rows[i]["br_name"].ToString());
                    }
                   }



You don't have to add it one by one. Change your above block of code to

C#
if(dt1.Rows.Count > 0)
{
ddl_brid.DataSource = dt1;
ddl_brid.DataTextField = "br_name";
ddl_brid.DataValueField = "br_id";
ddl_brid.DataBind();
}
 
Share this answer
 
v2
Comments
pallavi ajin 18-Oct-11 0:33am    
but in bind stmnt itys showing exception of object reference set to null
pallavi ajin 18-Oct-11 0:42am    
tell me sir plz
virang_21 18-Oct-11 1:08am    
Check updated solution.
pallavi ajin 18-Oct-11 4:01am    
thnk u so much u solved my prob i m so happy thnk u
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con;
SqlDataAdapter ad;
DataSet ds = new DataSet();
DataTable dt;

ad = new SqlDataAdapter("select strSectionName,intSectionId from JPCMstSection", (SqlConnection)Application.Get("con"));
ad.Fill(ds, "JPCMstSection");
dt = ds.Tables["JPCMstSection"];
for (int i = 0; i < dt.Rows.Count; i++)
{
dropdownlist1.Items.Add(dt.Rows[i]["strsectionName"].ToString());
dropdownlist1.Items[i].Value = dt.Rows[i]["intSectionId"].ToString();
}
ds.Clear();
dt.Clear();
return;
}
}

try this

Regards,
Anilkumar.D
 
Share this answer
 
v2

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