Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();

    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = "server=a2ul-pc;uid=sa;pwd=rascal;database=cgrt";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "insert into yearly (pcode,fyyear,yearlyalloc,salary,ta,contigency,nrc,institcharges,others) values (@pcode,@fyyear,@yearlyalloc,@salary,@ta,@contigency,@nrc,@institcharges,@others)";
        cmd.Parameters.AddWithValue("@pcode", DropDownList1.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@fyyear", TextBox10.Text +"-"+ TextBox2.Text);
        cmd.Parameters.AddWithValue("@yearlyalloc", Convert.ToInt32(TextBox3.Text));
        cmd.Parameters.AddWithValue("@salary", Convert.ToInt32(TextBox4.Text));
        cmd.Parameters.AddWithValue("@ta", Convert.ToInt32(TextBox5.Text));
        cmd.Parameters.AddWithValue("@contigency", Convert.ToInt32(TextBox6.Text));
        cmd.Parameters.AddWithValue("@nrc", Convert.ToInt32(TextBox7.Text));
        cmd.Parameters.AddWithValue("@institcharges", Convert.ToInt32(TextBox8.Text));
        cmd.Parameters.AddWithValue("@others",Convert.ToInt32(TextBox9.Text));

        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();

        DropDownList1.SelectedIndex = 0;
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        TextBox7.Text = "";
        TextBox8.Text = "";
        TextBox9.Text = "";

        Response.Write("<script type=\"text/javascript\">alert('Done!');</script>");
    }
}



[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 28-Oct-12 23:33pm
v2

Check that the columns of the table are compatible with the given data types. Also take a look for the lenght of strings and Int32 to smallint columns.
 
Share this answer
 
Comments
a2ulthakur 29-Oct-12 5:41am    
i checked the data type and its all ok :
USE [CGRT]
GO
/****** Object: Table [dbo].[yearly] Script Date: 10/29/2012 15:08:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[yearly](
[pcode] [varchar](50) NOT NULL,
[fyyear] [varchar](50) NOT NULL,
[yearlyalloc] [int] NOT NULL,
[salary] [int] NULL,
[ta] [int] NULL,
[contigency] [int] NULL,
[nrc] [int] NULL,
[institcharges] [int] NULL,
[others] [int] NULL,
[misc1] [varchar](50) NULL,
[misc2] [varchar](50) NULL,
[misc3] [varchar](50) NULL
) ON [PRIMARY]

this is my table script ? still after checking so many times i dont know whats wrong ?
BlackMilan 29-Oct-12 5:47am    
TextBox3.Text may be empty and yearlyalloc do not allow NULL value
usually this will happen because of "empty string converstion to int".

eg: Convert.ToInt32(TextBox.Text)); //here 'TextBox.Text' is empty.

Please make sure that all the "TextBox.Text" value is "not empty" before the Int32 conversion or use "int.TryParse".


Sample:

C#
int test1223 = -1;
         TextBox1.Text="2";
         int.TryParse(TextBox1.Text, out test1223); // 'test1223' value is "2"

         TextBox1.Text="";
         int.TryParse(TextBox1.Text, out test1223); //'test1223' value is "0"



Please refer the following link for more details about "int.TryParse" method.

http://msdn.microsoft.com/en-us/library/f02979c7.aspx[^]
 
Share this answer
 
v5
Comments
a2ulthakur 29-Oct-12 5:51am    
thanks it helped when i entered values in all textboxes but whats the way around it cause i dont need to fill up all the textboxes everytime. can u plz teach me how to use "int.TryPrase" method i have'nt used it ever before ?
It's pretty simple: the Convert.ToInt32 function is failing for one of your parameters because the TextBox value is not an integer - it may contain nothing, or an alphabetic character or similar.

I would strongly suggest that you convert these all to int values as the first part of the event handler using teh int.TryParse method, and report problems to your user so they know what to correct. Only when the values are good should you try to pass them to SQL.

Well done BTW on using parametrised queries! :thumbsup:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900