|
You have already posted this question below[^], and received a couple of answers.
Please DO NOT repost the same question.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I keep getting an error message, and i am not sure why.
Error:
The variable name '@No' has already been declared. Variable names must be unique within a query batch or stored procedure.
Here is my code:
cmd.Connection.Open();
foreach (ListViewItem item in lstViewXML.Items)
{
cmd.Parameters.Add("@No", item.Text);
cmd.Parameters.Add("@ItemNo", item.SubItems[1].Text);
cmd.Parameters.Add("@Qty", item.SubItems[2].Text);
cmd.CommandText = "Insert into xml (No,ItemNo,Qty) VALUES (@No,@ItemNo,@Qty)";
cmd.ExecuteNonQuery();
}
cmd.Connection.Close();
cmd.Dispose();
|
|
|
|
|
Add
cmd.Parameters.Clear
after
cmd.ExecuteNonQuery();
Also move
cmd.CommandText = "Insert into xml (No,ItemNo,Qty) VALUES (@No,@ItemNo,@Qty)";
out of the loop
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Thank you, it's working now.
Can you explain why i had to move
cmd.CommandText = "Insert into xml (No,ItemNo,Qty) VALUES (@No,@ItemNo,@Qty)";
Out of the loop?
|
|
|
|
|
It's a constant value - why set every time?!
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Thank you for your help
|
|
|
|
|
You welcome...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
hi, I am working in C#.net and taking ms-access as backend for windows application. I want to auto generate one number in a particular pattern(eg. AB/2014/2/2).
Here AB: will be some fixed string
2014: present year
2:month
5:auto generate no.(will increment after submission of form)
whenever some one will fill the form and will click on submit button then this no. has to be generated. last digit in this no. is sequence no. which will be incremented after checking previous no.
this no. will act as a primary key in database.
can somebody help me out with this? I am not getting how to code this.....
|
|
|
|
|
You can configure the DB to automatically set the primary key on insert of a new record.
For the rest, set a date field where you store the date of submission.
On display, you can display the date and the primary key together, e.g. 2014/2/1231
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
|
Diya Bh. wrote: and also I have to add constant string value in the beginning of the number.
You'd have to separate that into two rows, e.g.:
row_PrimaryKey ------------------ row_PrimaryKeyPrefix
[It's a number, after all ]
___________________________________________________________
Diya Bh. wrote: can you please explain me how to configure?
1. Google[^]
2. Look at some of the results[^]
That's all I can tell you so far, for any more info you need to give more information about what you are doing
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
ok.
user will fill the form and after clicking on submit button all the details will be stored in the database along with id number(this auto generated number) that will be generated automatically in the format AB/2014/2/6.
AB is fixed
2014 is present year
2 is present month
6 is a no. that will be incremented after checking previous sequence no.
this generated no. will act a primary key in whole database.
There will be no textbox for this no.
|
|
|
|
|
Diya Bh. wrote: this no. will act as a primary key in database. It shouldn't; it breaks the first normal form, as it is a compound field that contains multiple pieces of information. A Primary Key should identify the record uniquely, and should NEVER contain a constant.
One of the most important rules in database-design; the primary key does NEVER contain other information than to identify the field. The end-user NEVER sees the PK. Explaining why is simple; your number might change in the real world, and you'd be updating all references to that PK.
Put your fake-number (it's not a number as it contains characters) somewhere as a normal field, and make it a calculated field.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
this no. is uniquely defining fields in database.
Bcoz only string value is constant and rest of the field in the generated no. will be automatically from the present year and month and most imp.
last field in that no. is sequence no. which will get increment after checking previous no.
this means.. 3 fields in that auto generated no. will be combined and will make unique primary key.
|
|
|
|
|
Diya Bh. wrote: this means.
You already explained your intention - I merely remarked that it is a bad design. Primary keys should not hold information, and should always be a single atomic fact. Another point is that one does not repeat a constant in each value - it'd be nonsense to store a constant that applies to each row, with each row. It's wasting space, and making indexes larger (=slower db). If one needs a constant added to a field, one would generally add that when formatting it (in the UI).
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
You already have four answers for your question asked on the C# 4.0 forum: [^].
Why post it here, also ?
“But I don't want to go among mad people,” Alice remarked.
“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”
“How do you know I'm mad?” said Alice.
“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll
|
|
|
|
|
Is there a way of displaying SSRS report in MVC 4.
I googled and many articles are using reportviewer in webform and routing to that aspx page.
There should be a better way of doing this in MVC 4
|
|
|
|
|
|
the thing is my reports are created in SQL
either i can deploy them and use the link or i can use the RDLC file
which one is better solution and how to view existing report created in SQL
|
|
|
|
|
|
|
|
I used some barcode fonts and tried to implement code 128 to generate a barcode using those fonts using c#.net. But how to convert that barcode into image and save it into a local folder. I am new to c#.net.
|
|
|
|
|
Since you use specialised fonts, I'd suggest to try the Graphics.DrawString() method for drawing it into a e.g. bitmap.
|
|
|
|
|
Please use the following code,there is two classes and use can pass the barcodeString which you create the image or bitmap from string.
This function return you the Code128 format barcodes
using System;
using System.Text;
namespace BarcodeAPP
{
public enum CodeSet
{
CodeA
,CodeB
// ,CodeC // not supported
}
/// <summary>
/// Represent the set of code values to be output into barcode form
/// </summary>
public class Code128Content
{
private int[] mCodeList;
/// <summary>
/// Create content based on a string of ASCII data
/// </summary>
/// <param name="AsciiData">the string that should be represented</param>
public Code128Content( string AsciiData )
{
mCodeList = StringToCode128(AsciiData);
}
/// <summary>
/// Provides the Code128 code values representing the object's string
/// </summary>
public int[] Codes
{
get
{
return mCodeList;
}
}
/// <summary>
/// Transform the string into integers representing the Code128 codes
/// necessary to represent it
/// </summary>
/// <param name="AsciiData">String to be encoded</param>
/// <returns>Code128 representation</returns>
private int[] StringToCode128( string AsciiData )
{
// turn the string into ascii byte data
byte[] asciiBytes = Encoding.ASCII.GetBytes( AsciiData );
// decide which codeset to start with
Code128Code.CodeSetAllowed csa1 = asciiBytes.Length>0 ? Code128Code.CodesetAllowedForChar( asciiBytes[0] ) : Code128Code.CodeSetAllowed.CodeAorB;
Code128Code.CodeSetAllowed csa2 = asciiBytes.Length>0 ? Code128Code.CodesetAllowedForChar( asciiBytes[1] ) : Code128Code.CodeSetAllowed.CodeAorB;
CodeSet currcs = GetBestStartSet(csa1,csa2);
// set up the beginning of the barcode
System.Collections.ArrayList codes = new System.Collections.ArrayList(asciiBytes.Length + 3); // assume no codeset changes, account for start, checksum, and stop
codes.Add(Code128Code.StartCodeForCodeSet(currcs));
// add the codes for each character in the string
for (int i = 0; i < asciiBytes.Length; i++)
{
int thischar = asciiBytes[i];
int nextchar = asciiBytes.Length>(i+1) ? asciiBytes[i+1] : -1;
codes.AddRange( Code128Code.CodesForChar(thischar, nextchar, ref currcs) );
}
// calculate the check digit
int checksum = (int)(codes[0]);
for (int i = 1; i < codes.Count; i++)
{
checksum += i * (int)(codes[i]);
}
codes.Add( checksum % 103 );
codes.Add( Code128Code.StopCode() );
int[] result = codes.ToArray(typeof(int)) as int[];
return result;
}
/// <summary>
/// Determines the best starting code set based on the the first two
/// characters of the string to be encoded
/// </summary>
/// <param name="csa1">First character of input string</param>
/// <param name="csa2">Second character of input string</param>
/// <returns>The codeset determined to be best to start with</returns>
private CodeSet GetBestStartSet( Code128Code.CodeSetAllowed csa1, Code128Code.CodeSetAllowed csa2 )
{
int vote = 0;
vote += (csa1==Code128Code.CodeSetAllowed.CodeA) ? 1 : 0;
vote += (csa1==Code128Code.CodeSetAllowed.CodeB) ? -1 : 0;
vote += (csa2==Code128Code.CodeSetAllowed.CodeA) ? 1 : 0;
vote += (csa2==Code128Code.CodeSetAllowed.CodeB) ? -1 : 0;
return (vote>0) ? CodeSet.CodeA : CodeSet.CodeB; // ties go to codeB due to my own prejudices
}
}
/// <summary>
/// Static tools for determining codes for individual characters in the content
/// </summary>
public static class Code128Code
{
#region Constants
private const int cSHIFT = 98;
private const int cCODEA = 101;
private const int cCODEB = 100;
private const int cSTARTA = 103;
private const int cSTARTB = 104;
private const int cSTOP = 106;
#endregion
/// <summary>
/// Get the Code128 code value(s) to represent an ASCII character, with
/// optional look-ahead for length optimization
/// </summary>
/// <param name="CharAscii">The ASCII value of the character to translate</param>
/// <param name="LookAheadAscii">The next character in sequence (or -1 if none)</param>
/// <param name="CurrCodeSet">The current codeset, that the returned codes need to follow;
/// if the returned codes change that, then this value will be changed to reflect it</param>
/// <returns>An array of integers representing the codes that need to be output to produce the
/// given character</returns>
public static int[] CodesForChar(int CharAscii, int LookAheadAscii, ref CodeSet CurrCodeSet)
{
int[] result;
int shifter = -1;
if (!CharCompatibleWithCodeset(CharAscii,CurrCodeSet))
{
// if we have a lookahead character AND if the next character is ALSO not compatible
if ( (LookAheadAscii != -1) && !CharCompatibleWithCodeset(LookAheadAscii,CurrCodeSet) )
{
// we need to switch code sets
switch (CurrCodeSet)
{
case CodeSet.CodeA:
shifter = cCODEB;
CurrCodeSet = CodeSet.CodeB;
break;
case CodeSet.CodeB:
shifter = cCODEA;
CurrCodeSet = CodeSet.CodeA;
break;
}
}
else
{
// no need to switch code sets, a temporary SHIFT will suffice
shifter = cSHIFT;
}
}
if (shifter!=-1)
{
result = new int[2];
result[0] = shifter;
result[1] = CodeValueForChar(CharAscii);
}
else
{
result = new int[1];
result[0] = CodeValueForChar(CharAscii);
}
return result;
}
/// <summary>
/// Tells us which codesets a given character value is allowed in
/// </summary>
/// <param name="CharAscii">ASCII value of character to look at</param>
/// <returns>Which codeset(s) can be used to represent this character</returns>
public static CodeSetAllowed CodesetAllowedForChar(int CharAscii)
{
if (CharAscii>=32 && CharAscii<=95)
{
return CodeSetAllowed.CodeAorB;
}
else
{
return (CharAscii<32) ? CodeSetAllowed.CodeA : CodeSetAllowed.CodeB;
}
}
/// <summary>
/// Determine if a character can be represented in a given codeset
/// </summary>
/// <param name="CharAscii">character to check for</param>
/// <param name="currcs">codeset context to test</param>
/// <returns>true if the codeset contains a representation for the ASCII character</returns>
public static bool CharCompatibleWithCodeset(int CharAscii, CodeSet currcs)
{
CodeSetAllowed csa = CodesetAllowedForChar(CharAscii);
return csa==CodeSetAllowed.CodeAorB
|| (csa==CodeSetAllowed.CodeA && currcs==CodeSet.CodeA)
|| (csa==CodeSetAllowed.CodeB && currcs==CodeSet.CodeB);
}
/// <summary>
/// Gets the integer code128 code value for a character (assuming the appropriate code set)
/// </summary>
/// <param name="CharAscii">character to convert</param>
/// <returns>code128 symbol value for the character</returns>
public static int CodeValueForChar(int CharAscii)
{
return (CharAscii>=32) ? CharAscii-32 : CharAscii+64;
}
/// <summary>
/// Return the appropriate START code depending on the codeset we want to be in
/// </summary>
/// <param name="cs">The codeset you want to start in</param>
/// <returns>The code128 code to start a barcode in that codeset</returns>
public static int StartCodeForCodeSet(CodeSet cs)
{
return cs==CodeSet.CodeA ? cSTARTA : cSTARTB;
}
/// <summary>
/// Return the Code128 stop code
/// </summary>
/// <returns>the stop code</returns>
public static int StopCode()
{
return cSTOP;
}
/// <summary>
/// Indicates which code sets can represent a character -- CodeA, CodeB, or either
/// </summary>
public enum CodeSetAllowed
{
CodeA,
CodeB,
CodeAorB
}
}
}
-------------------------------------------------------------------------------------
secode class is
using System;
using System.Drawing;
using System.Diagnostics;
namespace BarcodeAPP
{
/// <summary>
/// Summary description for Code128Rendering.
/// </summary>
public static class Code128Rendering
{
#region Code patterns
// in principle these rows should each have 6 elements
// however, the last one -- STOP -- has 7. The cost of the
// extra integers is trivial, and this lets the code flow
// much more elegantly
private static readonly int[,] cPatterns =
{
{2,1,2,2,2,2,0,0}, // 0
{2,2,2,1,2,2,0,0}, // 1
{2,2,2,2,2,1,0,0}, // 2
{1,2,1,2,2,3,0,0}, // 3
{1,2,1,3,2,2,0,0}, // 4
{1,3,1,2,2,2,0,0}, // 5
{1,2,2,2,1,3,0,0}, // 6
{1,2,2,3,1,2,0,0}, // 7
{1,3,2,2,1,2,0,0}, // 8
{2,2,1,2,1,3,0,0}, // 9
{2,2,1,3,1,2,0,0}, // 10
{2,3,1,2,1,2,0,0}, // 11
{1,1,2,2,3,2,0,0}, // 12
{1,2,2,1,3,2,0,0}, // 13
{1,2,2,2,3,1,0,0}, // 14
{1,1,3,2,2,2,0,0}, // 15
{1,2,3,1,2,2,0,0}, // 16
{1,2,3,2,2,1,0,0}, // 17
{2,2,3,2,1,1,0,0}, // 18
{2,2,1,1,3,2,0,0}, // 19
{2,2,1,2,3,1,0,0}, // 20
{2,1,3,2,1,2,0,0}, // 21
{2,2,3,1,1,2,0,0}, // 22
{3,1,2,1,3,1,0,0}, // 23
{3,1,1,2,2,2,0,0}, // 24
{3,2,1,1,2,2,0,0}, // 25
{3,2,1,2,2,1,0,0}, // 26
{3,1,2,2,1,2,0,0}, // 27
{3,2,2,1,1,2,0,0}, // 28
{3,2,2,2,1,1,0,0}, // 29
{2,1,2,1,2,3,0,0}, // 30
{2,1,2,3,2,1,0,0}, // 31
{2,3,2,1,2,1,0,0}, // 32
{1,1,1,3,2,3,0,0}, // 33
{1,3,1,1,2,3,0,0}, // 34
{1,3,1,3,2,1,0,0}, // 35
{1,1,2,3,1,3,0,0}, // 36
{1,3,2,1,1,3,0,0}, // 37
{1,3,2,3,1,1,0,0}, // 38
{2,1,1,3,1,3,0,0}, // 39
{2,3,1,1,1,3,0,0}, // 40
{2,3,1,3,1,1,0,0}, // 41
{1,1,2,1,3,3,0,0}, // 42
{1,1,2,3,3,1,0,0}, // 43
{1,3,2,1,3,1,0,0}, // 44
{1,1,3,1,2,3,0,0}, // 45
{1,1,3,3,2,1,0,0}, // 46
{1,3,3,1,2,1,0,0}, // 47
{3,1,3,1,2,1,0,0}, // 48
{2,1,1,3,3,1,0,0}, // 49
{2,3,1,1,3,1,0,0}, // 50
{2,1,3,1,1,3,0,0}, // 51
{2,1,3,3,1,1,0,0}, // 52
{2,1,3,1,3,1,0,0}, // 53
{3,1,1,1,2,3,0,0}, // 54
{3,1,1,3,2,1,0,0}, // 55
{3,3,1,1,2,1,0,0}, // 56
{3,1,2,1,1,3,0,0}, // 57
{3,1,2,3,1,1,0,0}, // 58
{3,3,2,1,1,1,0,0}, // 59
{3,1,4,1,1,1,0,0}, // 60
{2,2,1,4,1,1,0,0}, // 61
{4,3,1,1,1,1,0,0}, // 62
{1,1,1,2,2,4,0,0}, // 63
{1,1,1,4,2,2,0,0}, // 64
{1,2,1,1,2,4,0,0}, // 65
{1,2,1,4,2,1,0,0}, // 66
{1,4,1,1,2,2,0,0}, // 67
{1,4,1,2,2,1,0,0}, // 68
{1,1,2,2,1,4,0,0}, // 69
{1,1,2,4,1,2,0,0}, // 70
{1,2,2,1,1,4,0,0}, // 71
{1,2,2,4,1,1,0,0}, // 72
{1,4,2,1,1,2,0,0}, // 73
{1,4,2,2,1,1,0,0}, // 74
{2,4,1,2,1,1,0,0}, // 75
{2,2,1,1,1,4,0,0}, // 76
{4,1,3,1,1,1,0,0}, // 77
{2,4,1,1,1,2,0,0}, // 78
{1,3,4,1,1,1,0,0}, // 79
{1,1,1,2,4,2,0,0}, // 80
{1,2,1,1,4,2,0,0}, // 81
{1,2,1,2,4,1,0,0}, // 82
{1,1,4,2,1,2,0,0}, // 83
{1,2,4,1,1,2,0,0}, // 84
{1,2,4,2,1,1,0,0}, // 85
{4,1,1,2,1,2,0,0}, // 86
{4,2,1,1,1,2,0,0}, // 87
{4,2,1,2,1,1,0,0}, // 88
{2,1,2,1,4,1,0,0}, // 89
{2,1,4,1,2,1,0,0}, // 90
{4,1,2,1,2,1,0,0}, // 91
{1,1,1,1,4,3,0,0}, // 92
{1,1,1,3,4,1,0,0}, // 93
{1,3,1,1,4,1,0,0}, // 94
{1,1,4,1,1,3,0,0}, // 95
{1,1,4,3,1,1,0,0}, // 96
{4,1,1,1,1,3,0,0}, // 97
{4,1,1,3,1,1,0,0}, // 98
{1,1,3,1,4,1,0,0}, // 99
{1,1,4,1,3,1,0,0}, // 100
{3,1,1,1,4,1,0,0}, // 101
{4,1,1,1,3,1,0,0}, // 102
{2,1,1,4,1,2,0,0}, // 103
{2,1,1,2,1,4,0,0}, // 104
{2,1,1,2,3,2,0,0}, // 105
{2,3,3,1,1,1,2,0} // 106
};
#endregion Code patterns
private const int cQuietWidth = 10;
/// <summary>
/// Make an image of a Code128 barcode for a given string
/// </summary>
/// <param name="InputData">Message to be encoded</param>
/// <param name="BarWeight">Base thickness for bar width (1 or 2 works well)</param>
/// <param name="AddQuietZone">Add required horiz margins (use if output is tight)</param>
/// <returns>An Image of the Code128 barcode representing the message</returns>
public static Image MakeBarcodeImage(string InputData, int BarWeight, bool AddQuietZone)
{
// get the Code128 codes to represent the message
Code128Content content = new Code128Content(InputData);
int[] codes = content.Codes;
int width, height;
width = ((codes.Length - 3) * 11 + 35) * BarWeight;
height = Convert.ToInt32(System.Math.Ceiling(Convert.ToSingle(width) * .22F));
if (AddQuietZone)
{
width += 2 * cQuietWidth * BarWeight; // on both sides
}
// get surface to draw on
Image myimg = new System.Drawing.Bitmap(width, height);
using (Graphics gr = Graphics.FromImage(myimg))
{
// set to white so we don't have to fill the spaces with white
gr.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height);
// skip quiet zone
int cursor = AddQuietZone ? cQuietWidth * BarWeight : 0;
for (int codeidx = 0; codeidx < codes.Length; codeidx++)
{
int code = codes[codeidx];
// take the bars two at a time: a black and a white
for (int bar = 0; bar < 8; bar += 2)
{
int barwidth = cPatterns[code, bar] * BarWeight;
int spcwidth = cPatterns[code, bar + 1] * BarWeight;
// if width is zero, don't try to draw it
if (barwidth > 0)
{
gr.FillRectangle(System.Drawing.Brushes.Black, cursor, 0, barwidth, height);
}
// note that we never need to draw the space, since we
// initialized the graphics to all white
// advance cursor beyond this pair
cursor += (barwidth + spcwidth);
}
}
}
return myimg;
}
}
}
In public static Image MakeBarcodeImage(string InputData, int BarWeight, bool AddQuietZone) Function
you can pass the barcodestring ,barcode weight and pass AddQuietZone as true.
Rahul S Chavhan
Pune
|
|
|
|
|