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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Jan 9, 2012, 5:55 AM
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
AartiPosted Jan 9, 2012, 5:17 AM
Try beloow line of code
Int32 a = Convert.ToInt32(textBox1.Text);Pravin GhadgePosted Jan 9, 2012, 5:12 AM
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.