Hi sir,
I have write above code but it show error when I Click on insert button.
The error is
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Cannot insert the value NULL into column 'CustomerId', table
I want that when I input data then there should be automatically generate id with bar code and save into database and all data with barcode id should goes to next page for print.
Sir I am really helpless by doing this.
namespace Dispatchwebform
{
public partial class defaultpage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void Send(object sender, EventArgs e)
{
GridViewRow gvRow = (sender as LinkButton).NamingContainer as GridViewRow;
Response.Redirect("print.aspx?Id=" + gvRow.Cells[0].Text);
}
protected void Insert(object sender, EventArgs e)
{
string name = txtName.Text;
string country = txtCountry.Text;
txtName.Text = "";
txtCountry.Text = "";
string constring = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("Insert into Customers (Name, Country) Values(@Name, @Country)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindGrid();
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
this.gvCustomers.DataSource = cmd.ExecuteReader();
this.gvCustomers.DataBind();
con.Close();
}
}
}
}
}
namespace Dispatchwebform
{
public partial class print : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("Select * From Customers WHERE CustomerId = @Id", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", Request.QueryString["Id"]);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
lblName.Text = sdr["Name"].ToString();
lblCountry.Text = sdr["Country"].ToString();
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(GenerateBarcode(sdr["CustomerId"].ToString()) as byte[]);
}
con.Close();
}
}
}
}
protected byte[] GenerateBarcode(string id)
{
byte[] byteImage;
using (Bitmap bitMap = new Bitmap(id.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M Free Version", 16);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + id + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byteImage = ms.ToArray();
}
}
return byteImage;
}
}
}