Shesh Mishra

Shesh Mishra

  • NA
  • 22
  • 31.8k

My COde Problem

Apr 22 2012 10:55 PM
Hello every I have a page name loanmaster.aspx ,the source code is given below:


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            margin-left: 8px;
        }
        .style2
        {
            width: 83%;
            height: 392px;
        }
        .style4
        {
            width: 216px;
        }
        .style6
        {
            height: 46px;
        }
        .style7
        {
            width: 216px;
            height: 50px;
        }
        .style8
        {
            height: 50px;
        }
        .style11
        {
            width: 216px;
            height: 55px;
        }
        .style12
        {
            height: 55px;
        }
        .style15
        {
            width: 216px;
            height: 48px;
        }
        .style16
        {
            height: 48px;
        }
        .style18
        {
            margin-left: 0px;
        }
        .style21
        {
            width: 216px;
            height: 46px;
        }
        .style22
        {
            height: 59px;
        }
        .style23
        {
            width: 216px;
            height: 58px;
        }
        .style24
        {
            height: 58px;
        }
        .style25
        {
            height: 65px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    

    <div>
    
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        Loan Master<br />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Panel ID="Panel1" runat="server" CssClass="style1" Height="472px"
                    Width="643px">
                    <asp:Label ID="lblmsg" runat="server"></asp:Label>
                    <br />
                    <br />
                    <table bgcolor="#F3FB9B" class="style2">
                        <tr>
                            <td bgcolor="#FFCC66" class="style15">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Loan Name *&nbsp;</td>
                            <td bgcolor="#FFCC66" class="style16">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:TextBox ID="txtloanname" runat="server" BackColor="#CCFFFF" Height="30px"
                                    ontextchanged="txtloanname_TextChanged" Width="200px"></asp:TextBox>
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td bgcolor="#FFCC66" class="style11">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Description&nbsp; *</td>
                            <td bgcolor="#FFCC66" class="style12">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:TextBox ID="txtdescription" runat="server" BackColor="#CCFFFF"
                                    Height="31px" ontextchanged="txtdescription_TextChanged" Width="199px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td bgcolor="#FFCC66" class="style7">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Min. Loan Amount&nbsp;*</td>
                            <td bgcolor="#FFCC66" class="style8">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:DropDownList ID="ddlminlamt" runat="server"
                                    BackColor="#CCFFFF" Height="28px"
                                    onselectedindexchanged="ddlminlamt_SelectedIndexChanged" Width="114px">
                                    <asp:ListItem></asp:ListItem>
                                    <asp:ListItem>10000</asp:ListItem>
                                    <asp:ListItem>50000</asp:ListItem>
                                    <asp:ListItem>100000</asp:ListItem>
                                    <asp:ListItem>150000</asp:ListItem>
                                    <asp:ListItem>200000</asp:ListItem>
                                    <asp:ListItem>250000</asp:ListItem>
                                    <asp:ListItem>300000</asp:ListItem>
                                    <asp:ListItem>350000</asp:ListItem>
                                    <asp:ListItem>400000</asp:ListItem>
                                    <asp:ListItem>450000</asp:ListItem>
                                    <asp:ListItem>500000</asp:ListItem>
                                    <asp:ListItem>600000</asp:ListItem>
                                    <asp:ListItem>700000</asp:ListItem>
                                    <asp:ListItem>800000</asp:ListItem>
                                    <asp:ListItem>900000</asp:ListItem>
                                    <asp:ListItem>1000000</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td bgcolor="#FFCC66" class="style25">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Max. Loan Amount &nbsp;*</td>
                            <td bgcolor="#FFCC66" class="style25">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:DropDownList ID="ddlmaxlamt" runat="server"
                                    BackColor="#CCFFFF" Height="28px"
                                    onselectedindexchanged="ddlmaxlamt_SelectedIndexChanged" Width="114px">
                                    <asp:ListItem>50000</asp:ListItem>
                                    <asp:ListItem>500000</asp:ListItem>
                                    <asp:ListItem>1000000</asp:ListItem>
                                    <asp:ListItem>2000000</asp:ListItem>
                                    <asp:ListItem>3000000</asp:ListItem>
                                    <asp:ListItem>4000000</asp:ListItem>
                                    <asp:ListItem>5000000</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td bgcolor="#FFCC66" class="style23">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Loan Interest *&nbsp;</td>
                            <td bgcolor="#FFCC66" class="style24">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<asp:TextBox ID="txtloaninterest" runat="server" BackColor="#CCFFFF"
                                    CssClass="style18" Height="20px" ontextchanged="txtloaninterest_TextChanged"
                                    Width="108px"></asp:TextBox>
                                &nbsp;in % Annual<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        </tr>
                        <tr>
                            <td bgcolor="#FFCC66" class="style7">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Loan Type *&nbsp;</td>
                            <td bgcolor="#FFCC66" class="style8">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<asp:DropDownList
                                    ID="ddlloantype" runat="server"
                                    BackColor="#CCFFFF" Height="50px" Width="111px"
                                    onselectedindexchanged="ddlloantype_SelectedIndexChanged">
                                    <asp:ListItem>Gold</asp:ListItem>
                                    <asp:ListItem>Education</asp:ListItem>
                                    <asp:ListItem>Home</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td bgcolor="#FFCC66" class="style21">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Remark&nbsp;</td>
                            <td bgcolor="#FFCC66" class="style6">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
                                <asp:TextBox ID="txtremark" runat="server" BackColor="#CCFFFF" Height="29px"
                                    TextMode="MultiLine" Width="197px" ontextchanged="txtremark_TextChanged"></asp:TextBox>
                                &nbsp;</td>
                        </tr>
                        <tr>
                            <td bgcolor="#FFCC66" class="style4">
                                &nbsp;</td>
                            <td bgcolor="#FFCC66">
                                <asp:Button ID="btnSubmit" runat="server" Height="37px"
                                    onclick="btnSubmit_Click" Text="Submit" Width="102px" />
                            </td>
                        </tr>
                    </table>
                </asp:Panel>
                <br />
<br />
<br />
            </ContentTemplate>
        </asp:UpdatePanel>
        <br />
        <br />
    
    </div>
    </form>
</body>
</html>
 
in it I have a c# code to submit the data using SQL SERVER 2008 while executing the page after filling the entry through the
submit button.i have an error in the code line{ a= cm.ExecuteNonQuery();} .The error is String or binary data would be truncated.
the statement has been terminated.

  Similarly while updating the database I have also seen this error.Sir please tell me how to solve it.

The C# code is as below :

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class loanmaster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            clrctl();

            if (Session["mode"].ToString() == "update")
            {
                string sql = "select loanname,description,minlamt,maxlamt,loaninterest,loantype,remark from loaninfo where id=" + Session["id"].ToString() + "";

                SqlDataAdapter da = new SqlDataAdapter(sql, cnn);
                DataSet ds = new DataSet();
                da.Fill(ds, "a");
                DataTable dt = ds.Tables["a"];
                foreach (DataColumn col in dt.Columns)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        txtloanname.Text = row[0].ToString();
                        txtdescription.Text = row[1].ToString();
                        ddlminlamt.Text = row[2].ToString();
                        ddlmaxlamt.Text = row[3].ToString();
                        txtloaninterest.Text = row[4].ToString();
                        ddlloantype.Text = row[5].ToString();
                        txtremark.Text = row[6].ToString();
                        break;
                    }
                }
            }
        }
    }
    //gd
    SqlConnection cnn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=LMS;Data Source=SHESH-PC\SHESHSQLEXPRESS");
 
    public void clrctl()
    {
        txtloanname.Text = "";
        txtdescription.Text = "";
       // ddlminlamt.Text = "";
       // ddlmaxlamt.Text = "";
        txtloaninterest.Text = "";
       // ddlloantype.Text = "";
        txtremark.Text = "";
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

       /* if (txtloanname.Text == "")
        {
            lblmsg.Text = "Enter the loan name";
            txtloanname.Focus();
            return;
        }
        if (txtdescription.Text == "")
        {
            lblmsg.Text = "Enter the description";
            txtdescription.Focus();
            return;
        }
       
        if (ddlminlamt.Text == "")
        {
            lblmsg.Text = "Enter the minimum loan amount";
            ddlminlamt.Focus();
            return;
        }
        if (ddlmaxlamt.Text == "")
        {
            lblmsg.Text = "Enter the maximum loan amount";
            ddlminlamt.Focus();
            return;
        }
       
        if (txtloaninterest.Text == "")
        {
            lblmsg.Text = "Enter the interest of loan";
            txtloaninterest.Focus();
            return;
        }
        if (ddlloantype.Text == "")
        {
            lblmsg.Text = "Enter the type of loan";
            ddlminlamt.Focus();
            return;
        }
       
        if (txtremark.Text == "")
        {
            lblmsg.Text = "Enter the Remark.";
            txtremark.Focus();
            return;
        }*/
        if (Session["mode"].ToString() == "save")
        {
            //insert into
            string sql = "insert into loaninfo(loanname,description,minlamt,maxlamt,loaninterest,loantype,remark,entrydate,entryperson)values('" + txtloanname.Text + "','" + txtdescription.Text + "','" + ddlminlamt.SelectedItem.ToString() + "','" + ddlmaxlamt.SelectedItem.ToString() + "', " + txtloaninterest.Text + ",'" + ddlloantype.SelectedItem.ToString() + "' ,'" + txtremark.Text + "','" + DateTime.Now.ToString() + "','" + Session["loginname"].ToString() + "')";
            SqlCommand cm = new SqlCommand();

            cm.Connection = cnn;
            cm.CommandType = CommandType.Text;
            cm.CommandText = sql;
            int a = 0;
            cnn.Open();
            a = cm.ExecuteNonQuery();
            cnn.Close();
            if (a == 0)
            {
                lblmsg.Text = "Data is not saved.";
            }
            else
            {
                lblmsg.Text = "Data is saved.";
                clrctl();
            }
         }
         else
        {
            //update the loaninfo
            string sql ="update loaninfo set loanname ='"+txtloanname+"',description ='"+txtdescription.Text+"',minlamt='"+ddlminlamt.SelectedItem.ToString()+"',maxlamt='"+ddlmaxlamt.SelectedItem.ToString()+"',loaninterest="+txtloaninterest.Text+",loantype='"+ ddlloantype.SelectedItem.ToString() +"',remark='"+txtremark.Text+"'where id ="+ Session["id"].ToString() +"";
            SqlCommand cm = new SqlCommand();
            cm.Connection = cnn;
            cm.CommandType = CommandType.Text;
            cm.CommandText = sql;
            int a = 0;
            cnn.Open();
            a = cm.ExecuteNonQuery();
            cnn.Close();
            if (a == 0)
            {
                lblmsg.Text = "DATA NOT UPDATED";
            }
            else
            {

              lblmsg.Text = "DATA UPDATED";
              clrctl();
            }
        }
  }

    protected void txtloanname_TextChanged(object sender, EventArgs e)
    {

    }
    protected void txtdescription_TextChanged(object sender, EventArgs e)
    {

    }
    protected void ddlminlamt_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ddlmaxlamt_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void txtloaninterest_TextChanged(object sender, EventArgs e)
    {

    }
    protected void ddlloantype_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void txtremark_TextChanged(object sender, EventArgs e)
    {

    }
}

Answers (3)