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

Dani AI

Generated

The exception came from trying to convert a non-numeric string into an int and from reading control objects rather than their values. As pointed out, the dropdown's selected value must be used; confirmed that switching to the dropdown value fixed the immediate error. A few extra, practical hardening tips follow.

Validate the dropdown selection before parsing. Use a safe parse (so a bad or placeholder value does not throw) and return a friendly message if the user did not pick a valid class:

int hisClass;
if (!int.TryParse(ddlClasses.SelectedValue, out hisClass))
{
    dbErrorLabel.Text = "Please select a valid class.";
    return;
}

Read text from TextBox controls with their Text property (not ToString() on the control). Use using blocks for connection/command cleanup, set CommandType to the stored-proc, and match parameter types to the procedure (the proc uses nvarchar, so use SqlDbType.NVarChar and set the size). Example pattern:

using (var conn = new SqlConnection(DBconn))
using (var cmd = new SqlCommand("AddNewEducator", conn) { CommandType = CommandType.StoredProcedure })
{
    cmd.Parameters.Add("@EducatorName", SqlDbType.NVarChar, 20).Value = tbEducatorName.Text;
    cmd.Parameters.Add("@EducatorFamilyname", SqlDbType.NVarChar, 20).Value = tbEducatorFamilyname.Text;
    cmd.Parameters.Add("@EducatorClass", SqlDbType.Int).Value = hisClass;
    conn.Open();
    cmd.ExecuteNonQuery();
}

Also ensure the DropDownList's DataValueField is actually a numeric ID (or handle non-numeric placeholders), validate server-side, and log exceptions for troubleshooting. For reference, see the docs on ListControl.SelectedValue and Int32.TryParse.

Recommended Answers

All 3 Replies

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.