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">
Loan Name * </td>
<td bgcolor="#FFCC66" class="style16">
<asp:TextBox ID="txtloanname" runat="server" BackColor="#CCFFFF" Height="30px"
ontextchanged="txtloanname_TextChanged" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td bgcolor="#FFCC66" class="style11">
Description *</td>
<td bgcolor="#FFCC66" class="style12">
<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">
Min. Loan Amount *</td>
<td bgcolor="#FFCC66" class="style8">
<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">
Max. Loan Amount *</td>
<td bgcolor="#FFCC66" class="style25">
<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">
Loan Interest * </td>
<td bgcolor="#FFCC66" class="style24">
<br /> <asp:TextBox ID="txtloaninterest" runat="server" BackColor="#CCFFFF"
CssClass="style18" Height="20px" ontextchanged="txtloaninterest_TextChanged"
Width="108px"></asp:TextBox>
in % Annual<br /> </td>
</tr>
<tr>
<td bgcolor="#FFCC66" class="style7">
Loan Type * </td>
<td bgcolor="#FFCC66" class="style8">
<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">
Remark </td>
<td bgcolor="#FFCC66" class="style6">
<asp:TextBox ID="txtremark" runat="server" BackColor="#CCFFFF" Height="29px"
TextMode="MultiLine" Width="197px" ontextchanged="txtremark_TextChanged"></asp:TextBox>
</td>
</tr>
<tr>
<td bgcolor="#FFCC66" class="style4">
</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)
0
hi,
mostly this error comes when your size of input is exceed.
Example I have one table and it has one column say name and size of name column is varchar(20), now i am trying to insert name in this table with length 25. at that time sql server throw this error.
So recommended that chat your input and database size.
hope this help.
Accepted 0
Hi,
The reason for this error is clear.
When you set the size to your column in the sql server table, then you need to do insert/update with the required size of the data.
Support, you have a table called register
CREATE TABLE Register
(
ID INT IDENTITY(1,1)
,Name VARCHAR(10)
)
INSERT INTO Register(Name) VALUES('AAAAABBBBBBCCCCCC')
Here the actual size of the Name column is 10 and you are trying to insert 15 chars, then you will get string binary truncated.
0
thank you sir I will try it.