Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void bind()
     {
         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);

         SqlDataAdapter da = new SqlDataAdapter("select * from employee1", con);
         DataSet ds = new DataSet();


         da.Fill(ds);

         datalist1.DataSource = ds;
         datalist1.DataBind();
     }

     protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
     {

     }

     protected void datalist1_ItemDataBound(object sender, DataListItemEventArgs e)
     {
         if (e.Item.ItemType == ListItemType.Footer)
         {

             TextBox textid = e.Item.FindControl("textid") as TextBox;

             TextBox textname = e.Item.FindControl("textname") as TextBox;
             TextBox textaddress = e.Item.FindControl("textaddress") as TextBox;
             TextBox texteducation = e.Item.FindControl("texteducation") as TextBox;
             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
             SqlCommand cmd = new SqlCommand("Insert into employee1(emp_id,emp_name,emp_address,emp_education values(@1,@2,@3,@4)",con);
             cmd.Parameters.Add("@1", SqlDbType.Int).Value = Convert.ToInt32(textid.Text);
             cmd.Parameters.Add("@2", SqlDbType.NChar,10).Value = textname.Text;
             cmd.Parameters.Add("@3", SqlDbType.NChar,10).Value = textaddress.Text;
             cmd.Parameters.Add("@4", SqlDbType.NChar,10).Value=texteducation.Text;
             con.Open();
             cmd.ExecuteNonQuery();
             con.Close();
             bind();


[Update]
Server Error in '/' Application.

Input string was not in a correct format.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 59: SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
Line 60: SqlCommand cmd = new SqlCommand("Insert into employee1(emp_id,emp_name,emp_address,emp_education values(@1,@2,@3,@4)",con);
Line 61: cmd.Parameters.Add("@1", SqlDbType.Int).Value = Convert.ToInt32("textid.Text");
Line 62: cmd.Parameters.Add("@2", SqlDbType.NChar,10).Value = textname.Text;
Line 63: cmd.Parameters.Add("@3", SqlDbType.NChar,10).Value = textaddress.Text;

Source File: C:\Users\Vishal\downloads\documents\visual studio 2010\Projects\WebApplication2\WebApplication2\datalist.aspx.cs Line: 61

Stack Trace:


[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +11177559
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +145
System.Convert.ToInt32(String value) +43
WebApplication2.WebForm1.datalist1_ItemDataBound(Object sender, DataListItemEventArgs e) in C:\Users\Vishal\downloads\documents\visual studio 2010\Projects\WebApplication2\WebApplication2\datalist.aspx.cs:61
System.Web.UI.WebControls.DataList.OnItemDataBound(DataListItemEventArgs e) +9579420
System.Web.UI.WebControls.DataList.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +151
System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource) +9580357
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +63
System.Web.UI.WebControls.BaseDataList.DataBind() +54
WebApplication2.WebForm1.bind() in C:\Users\Vishal\downloads\documents\visual studio 2010\Projects\WebApplication2\WebApplication2\datalist.aspx.cs:39
WebApplication2.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Users\Vishal\downloads\documents\visual studio 2010\Projects\WebApplication2\WebApplication2\datalist.aspx.cs:19
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
Posted
Updated 19-Nov-15 20:33pm
v2
Comments
Garth J Lancaster 20-Nov-15 0:49am    
where is it giving you the error ? have you put a breakpoint in above this [error point] and stepped down to the error point inspecting the variables ?
PIEBALDconsult 20-Nov-15 0:57am    
Probably in Convert.ToInt32(textid.Text)
Never use Convert; try using Int32.TryParse instead.
And use the debugger to see what the value is.
[no name] 20-Nov-15 1:23am    
Match the column data type with provided parameter type. Might be there will be issue with Convert.ToInt32(textid.Text). Post your full error message and stack trace.
vishal goyal 20-Nov-15 1:42am    
Server Error in '/' Application.

Input string was not in a correct format.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 59: SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
Line 60: SqlCommand cmd = new SqlCommand("Insert into employee1(emp_id,emp_name,emp_address,emp_education values(@1,@2,@3,@4)",con);
Line 61: cmd.Parameters.Add("@1", SqlDbType.Int).Value = Convert.ToInt32("textid.Text");
Line 62: cmd.Parameters.Add("@2", SqlDbType.NChar,10).Value = textname.Text;
Line 63: cmd.Parameters.Add("@3", SqlDbType.NChar,10).Value = textaddress.Text;

Source File: C:\Users\Vishal\downloads\documents\visual studio 2010\Projects\WebApplication2\WebApplication2\datalist.aspx.cs Line: 61

Stack Trace:


[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +11177559
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +145
System.Convert.ToInt32(String value) +43
WebApplication2.WebForm1.datalist1_ItemDataBound(Object sender, DataListItemEventArgs e) in C:\Users\Vishal\downloads\documents\visual studio 2010\Projects\WebApplication2\WebApplication2\datalist.aspx.cs:61
System.Web.UI.WebControls.DataList.OnItemDataBound(DataListItemEventArgs e) +9579420
System.Web.UI.WebControls.DataList.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +151
System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource) +9580357
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +63
System.Web.UI.WebControls.BaseDataList.DataBind() +54
WebApplication2.WebForm1.bind() in C:\Users\Vishal\downloads\documents\visual studio 2010\Projects\WebApplication2\WebApplication2\datalist.aspx.cs:39
WebApplication2.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Users\Vishal\downloads\documents\visual studio 2010\Projects\WebApplication2\WebApplication2\datalist.aspx.cs:19
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
[no name] 20-Nov-15 2:29am    
Debug the value for textid.Text.Probable Its not an integer while you converting it.

Check the various database types versus what you are passing into the query.
There is a type mismatch between what you send versus the parameters / table schema.
 
Share this answer
 
C#
TextBox textid = e.Item.FindControl("textid") as TextBox;
  int id = int.Parse(textid.Text);
  cmd.Parameters.Add("@1", SqlDbType.Int).Value = id;


First Open the cOnnection

C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
con.Open();
Query
Con.Close();
 
Share this answer
 
v2
Comments
Arasappan 20-Nov-15 2:21am    
whats happen when using this solution
vishal goyal 20-Nov-15 2:30am    
showing same error
Arasappan 20-Nov-15 2:30am    
Dd u use break point
vishal goyal 20-Nov-15 2:33am    
yes..
Arasappan 20-Nov-15 2:37am    
Wheres the error appear;
Problem is in this line

C#
SqlCommand cmd=new SqlCommand(Insert into
employee1(emp_id,emp_name,emp_address,emp_education values(@1,@2,@3,@4)",con);


Replace with

C#
SqlCommand cmd=new SqlCommand(Insert into
employee1(emp_id,emp_name,emp_address,emp_education) values(@1,@2,@3,@4)",con);




You need to close the brace ")" after column declaration completed, after closing if you still face the same problem then I request you to please compare your database datatypes and your code datatypes, both should be same.
 
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