Hello,

My code is:

protected void Button1_Click(object sender, EventArgs e)
    {
        string EducatorName = tbEducatorName.ToString();
        string Educatorfamilyname = tbEducatorFamilyname.ToString();
        int Hisclass =Int32.Parse(ddlClasses.DataValueField.ToString());

        SqlConnection conn = new SqlConnection(DBconn);
        SqlCommand cmd = new SqlCommand("AddNewEducator", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "AddNewEducator";
        cmd.Parameters.Add("@EducatorName", SqlDbType.VarChar).Value = EducatorName;
        cmd.Parameters.Add("@Educatorfamilyname", SqlDbType.VarChar).Value = Educatorfamilyname;
        cmd.Parameters.Add("@EducatorClass", SqlDbType.Int).Value = Hisclass;
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            dbErrorLabel.Text = ex.Message.ToString();
        }
        finally
        {
            conn.Close();
        }
    }

Stored Procedure:

create procedure [dbo].[AddNewEducator]
@EducatorName nvarchar(20),
@EducatorFamilyname nvarchar(20),
@EducatorClass int
as
insert into Educators
(
EducatorName,
EducatorFamilyname,
EducatorClass
)
values
(
@EducatorName,
@EducatorFamilyname,
@EducatorClass
)

well, when i try to execute this method it says

int Hisclass =Int32.Parse(ddlClasses.DataValueField.ToString());

Format exception.

cmd.Parameters.Add("@EducatorClass", SqlDbType.Int).Value = Hisclass;

and this is a value I want to get from dropdownlist and insert it into DB

Please help

Int32.Parse() will throw Format exception, if the values is other than integer.

int Hisclass =Int32.Parse(ddlClasses.DataValueField.ToString());

Do you mean to use int Hisclass =Int32.Parse(ddlClasses.SelectedValue);?

Int32.Parse() will throw Format exception, if the values is other than integer.


Do you mean to use int Hisclass =Int32.Parse(ddlClasses.SelectedValue);?

Yes

Yes

Ok I've solved it by myself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.