1

I have created two table in SQL server they are:

TblStudent:

TblStudent

TblProvince:

TblProvince

In Asp.net Design Form:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="StudentName"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtStudentName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Province"></asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlProvince" runat="server"></asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

In Asp.net Back-End :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //bound data from TblProvince to ddlProvince 
            using (SqlConnection con  = DBManager.getConnection())
            {
                string sql = @"Select ProvinceId, Province From TblProvince";
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                da.Fill(ds, "dtProvince");
                ddlProvince.DataSource = ds.Tables["dtProvince"];
                ddlProvince.DataValueField = "ProvinceId";
                ddlProvince.DataTextField = "Province";
                ddlProvince.DataBind();
                ddlProvince.Items.Insert(0, new ListItem("Please Select", "NULL"));
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = DBManager.getConnection())
        {
            string sql = @"Insert Into TblStudent(Name, ProvinceId) Values(@Name, @ProvinceId)";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.Add("@Name", SqlDbType.NChar).Value = txtStudentName.Text;
            cmd.Parameters.Add("@ProvinceId", SqlDbType.Int).Value = Convert.ToInt32(ddlProvince.SelectedValue);
            con.Open();
            cmd.ExecuteNonQuery();
        }
    }
}

Question:
When I do not select in ddlProvince why when u save it to database it shows an error Input string was not in a correct format.

5
  • 1
    what is the error u are getting and what is your question, i know you mentioned your qst, can you expand a bit Commented Mar 22, 2016 at 4:56
  • please check this solution on save click try to check if(ddlprovince.SelectedValue !=null){//enter your save operation} else{//through a msg to select a dropdown value} Commented Mar 22, 2016 at 5:01
  • Ok !! When i save data to database without select dropdownlist it show error "Input string was not in a correct format." but when i select other item in dropdownlist it nothing error Commented Mar 22, 2016 at 6:10
  • did you tried my approach? what my approach says, if the ddl value is null , DB will not hit so there are less chances of getting null value to your Table Commented Mar 22, 2016 at 6:16
  • Ok thank you let me try again... Commented Mar 22, 2016 at 8:39

1 Answer 1

1

It throws "Input string was not in a correct format" error because you are trying to convert null to Int.

cmd.Parameters.Add("@ProvinceId", SqlDbType.Int).Value = Convert.ToInt32(ddlProvince.SelectedValue);

You can check whether SelectedValue is null before converting it to Int as below.

cmd.Parameters.Add("@ProvinceId", SqlDbType.Int).Value = (ddlProvince.SelectedValue == null) ? null :  Convert.ToInt32(ddlProvince.SelectedValue)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.