Error covert string to int
I make one GUI form. i which i make 3 textboxes. i first textbox the no. is genrated automatically becasue i use identity in SQL. but how to display this identity no. in my first textboxe.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pravin GhadgeRank #1,340 · 1.3K reputationPosted Jan 9, 2012, 12:12 AM EST
ur question is not clear for me.
u want to display identity value after saving the data in sql. am i correct?
If yes then,
use datareader or adapter to fetch the record.
AartiRank #2,383 · 594 reputationPosted Jan 9, 2012, 12:17 AM EST
Try beloow line of code
Int32 a = Convert.ToInt32(textBox1.Text);Satyapriya NayakRank #47 · 31.5K reputationPosted Jan 9, 2012, 12:55 AM EST
Try this..
Create table employee (empid varchar(50),empname varchar(50),sal int)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
int count;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
autogenerated();
}
}
void autogenerated()
{
SqlConnection con = new SqlConnection(strConnString);
str = "select count(*) from employee";
com = new SqlCommand(str, con);
con.Open();
count = Convert.ToInt16(com.ExecuteScalar()) + 1;
txt_empid.Text = count.ToString();
txt_empid.Enabled = false;
con.Close();
}
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "insert into employee values('" + txt_empid.Text.Trim() + "','" + txt_empname.Text.Trim() + "'," + txt_sal.Text.Trim() + ")";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Label4.Text = "Records successfully Inserted";
txt_empname.Text = "";
txt_sal.Text = "";
autogenerated();
}
}
Thanks
If this post helps you mark it as answer