0

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

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Nfurman
0

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);?

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.