WebForm2.aspx
<form id="form1" runat="server">
<div>
<asp:Button ID="btnGenerate" runat="server" Text="Generate Random Number" OnClick="btnGenerate_Click" />
<br /><br />
<asp:Label ID="lblRandomNumber" runat="server" Text=""></asp:Label>
</div>
</form>
<div>
<asp:Button ID="btnGenerate" runat="server" Text="Generate Random Number" OnClick="btnGenerate_Click" />
<br /><br />
<asp:Label ID="lblRandomNumber" runat="server" Text=""></asp:Label>
</div>
</form>
using System.Data.SqlClient;
using System.Text;
using System.Configuration;
using System.Text;
using System.Configuration;
WebForm2.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGenerate_Click(object sender, EventArgs e)
{
// Generate a random 10-digit number
Random random = new Random();
string randomNumber = random.Next(1000000000, 1999999999).ToString();
// Display the random number in the label
lblRandomNumber.Text = "Generated Number: " + randomNumber;
// Insert the random number into the database
InsertRandomNumberIntoDatabase(randomNumber);
}
private void InsertRandomNumberIntoDatabase(string randomNumber)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = "INSERT INTO tab (phonen) VALUES (@phonen)";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@phonen", randomNumber);
conn.Open();
cmd.ExecuteNonQuery();
}
}
}
{
}
protected void btnGenerate_Click(object sender, EventArgs e)
{
// Generate a random 10-digit number
Random random = new Random();
string randomNumber = random.Next(1000000000, 1999999999).ToString();
// Display the random number in the label
lblRandomNumber.Text = "Generated Number: " + randomNumber;
// Insert the random number into the database
InsertRandomNumberIntoDatabase(randomNumber);
}
private void InsertRandomNumberIntoDatabase(string randomNumber)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = "INSERT INTO tab (phonen) VALUES (@phonen)";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@phonen", randomNumber);
conn.Open();
cmd.ExecuteNonQuery();
}
}
}
Web.Config
<add name="ConnectionString" connectionString="Data Source=MSSQLLocalDB;AttachDbFilename=yourdatabasename;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
---------
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnsave" runat="server" Text="save" OnClick="btnsave_Click" />
</div>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
generatesno();
}
}
private void generatesno()
{
String mycon = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True";
SqlConnection scon = new SqlConnection(mycon);
String myquery = "select addresse from Table_1";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = scon;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
scon.Close();
if (ds.Tables[0].Rows.Count < 1)
{
TextBox1.Text = "DS001";
}
else
{
String mycon1 = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True";
SqlConnection scon1 = new SqlConnection(mycon1);
String myquery1 = "select addresse from Table_1";
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandText = myquery1;
cmd1.Connection = scon1;
SqlDataAdapter da1 = new SqlDataAdapter();
da1.SelectCommand = cmd1;
DataSet ds1 = new DataSet();
da1.Fill(ds1);
int maxsno = 0;
var part1 = "0";
var part2 = "0";
foreach (DataRow dr in ds.Tables[0].Rows)
{
var sno = dr["addresse"].ToString();
part1 = sno.Substring(0, 2);
part2 = sno.Substring(2, (sno.Length) - 2);
if (maxsno < Convert.ToInt16(part2))
{
maxsno = Convert.ToInt16(part2);
}
}
maxsno = maxsno + 1;
var newserial = part1 + maxsno.ToString("000");
TextBox1.Text = newserial.ToString();
scon1.Close();
}
}
protected void btnsave_Click(object sender, EventArgs e)
{
//string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
string filename = TextBox1.Text; // + extension
//FileUpload1.SaveAs(Server.MapPath("~/UploadedFiles/") + filename);
string query = "insert into Table_1(addresse) values('" + TextBox1.Text + "'')"; //,'" + TextBox2.Text + "','UploadedFiles/" + filename + "
String mycon = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True";
SqlConnection con = new SqlConnection(mycon);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = query;
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
Label3.Text = "Record Has Been Saved Successfully";
TextBox1.Text = "";
TextBox2.Text = "";
generatesno();
}
0 Comments