Write to CSV with button. Used for Bartender

Brandon,

That’s super easy! It has a few steps:

  1. Determine what printer (or allow user to select a printer)
  2. Compile the data you want on your label (to pass to bartender) - In you case, sounds like you already have your data in a grid so we just snatch it off there and pass it to…
  3. Function that takes your data and concatenates it into the CSV
  4. Write file to proper BT location that is HEADER SCV FOOTER

Yep - you can do it all in that button click. I’ll share code with you if you like, just don’t let Jose see it cuz he’ll beat me up #PastaProgrammer

//This takes all compiled csv's and selects # of copies, printer, label template, and seed doesn't matter but it ensures a unique bartender filename (which I needed for placing multiple trigger files at once)
private void PrintLabel(string headers, string datas, int copies, string printer, string label, int seed)
	{  //Bartender Header
		string text = string.Format("%BTW% /AF=\"{0}\" /D=\"<Trigger File Name>\" /PRN=\"{1}\"  /R=3 /C={2} /P ", label, printer, copies);
		string[] contents = new string[]
		{
			text,
			"%END%",  //bartender footer
			headers,
			datas
		};
		System.IO.File.WriteAllLines("\\\\UrServer\\c$\\Bartender\\STOCK"+seed.ToString()+".bt", contents);
	}

//This takes all the data I wanted to print and turns it into a CSV (and makes data headers too since I have my BT setup to use them)
	private void MakeLabelData(string pn, string desc, string qty, string matpn, string matdes, string date, string emp, string rev, string job, string dev, string boxnum, string ppc, string wipdes, string poref, string spare)
	{
		this.headers = string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\",\"{11}\",\"{12}\",\"{13}\",\"{14}\"", new object[]
		{
			"pn",
			"desc",
			"qty",
			"matpn",
			"matdes",
			"date",
			"emp",
			"rev",
			"job",
			"dev",
			"boxnum",
			"ppc",
			"wipdes",
			"poref",
			"spare"
		});
		this.datas = string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\",\"{11}\",\"{12}\",\"{13}\",\"{14}\"", new object[]
		{
			pn,
			desc,
			qty,
			matpn,
			matdes,
			date,
			emp,
			rev,
			job,
			dev,
			boxnum,
			ppc,
			wipdes,
			poref,
			spare
		});
	}

//In my case I cycled 3 rows on a grid and created a trigger file for each
private void epiButtonPrint_Click(object sender, EventArgs args)
	{
		string pn = this.epiUltraComboPart.Text;
		for (int i = 0; i < 3; i++)
		{
			int lblqty = (int)this.LabelGrid.Rows[i].Cells[0].Value;
			string partqty = (((int)this.LabelGrid.Rows[i].Cells[1].Value)).ToString();
			if (lblqty > 0 && partqty != null && partqty != "" && partqty != "0")
			{
				//I didn't use all the fields on this particular label so I just put some place holders
				string label = string.Format("C:\\Bartender\\Formats\\cc\\StockLabel.btw", new object[0]);
				this.MakeLabelData(pn, "desc", partqty, "", "", "", ((Session)this.oTrans.Session).EmployeeID, "", "j", "d", "b", "ppc", "wipdes", "poref", "spare");
				this.PrintLabel(this.headers, this.datas, lblqty, this.prntr, label, i);
			}
			//else MessageBox.Show("Failed - lblqty: "+lblqty.ToString() +"  partqty: "+partqty);
		}
	}

For printers, I linked printers to workstations. This is my default printer. I use a BAQ to get that printer based on the current workstation. I also use a BAQ to list all available printers in the grid in my image. The default (or selected) is highlighted but the user can click to select any printer.

	private void GetDefaultPrinter()
	{
		DynamicQueryAdapter dynamicQueryAdapter = new DynamicQueryAdapter(this.oTrans);
		dynamicQueryAdapter.BOConnect();
		QueryExecutionDataSet queryExecutionParametersByID = dynamicQueryAdapter.GetQueryExecutionParametersByID("CC_WORKSTATION_PRINTER");
		queryExecutionParametersByID.ExecutionParameter.Clear();
		queryExecutionParametersByID.ExecutionParameter.AddExecutionParameterRow("WokstationIDParam", ((Session)this.oTrans.Session).WorkstationID, "nvarchar", false, Guid.NewGuid(), "A");
		dynamicQueryAdapter.ExecuteByID("CC_WORKSTATION_PRINTER", queryExecutionParametersByID);
		if (dynamicQueryAdapter.QueryResults.Tables["Results"].Rows.Count > 0)
		{
			this.prntr = dynamicQueryAdapter.QueryResults.Tables["Results"].Rows[0]["SysPrinter_NetworkPath"].ToString();
			return;
		}
		MessageBox.Show("No printers configured. Contact C.Conn");
	}
4 Likes