This question already has answers here:
The name '__o' does not exist in the current context
(6 answers)
Closed 6 years ago.
public void Button_Click(object sender, RoutedEventArgs e)
{
string stringConnectionString = null;
SqlConnection objSqlConnection;
SqlCommand objSqlCommand = new SqlCommand();
DataSet objDataSet = new DataSet();
string stringCommandText = string.Empty;
stringConnectionString = ("Data Source=192.161.0.0;Initial Catalog=kkh_final;Persist Security Info=True;User ID=xx;Password=yyyyyyy");
objSqlConnection = new SqlConnection(stringConnectionString);
objSqlConnection.Open();
objSqlCommand.Connection = objSqlConnection;
objSqlCommand.CommandType = CommandType.StoredProcedure;
objSqlCommand.CommandText = "PCL0105A0_MR_Patients";
}
public void clsPatients(DataRow objDataRow, string[] stringInfo, string stringCommandText)
{
SqlParameter[] objSqlParameter;
stringCommandText = "PCL0105A0_MR_Patients";
int intRowIndex;
DataSet objDataSet = new DataSet();
SqlCommand objSqlCommand = new SqlCommand();
SqlDataAdapter objSqlDataAdapter;
try
{
objSqlParameter = new SqlParameter[63];
objSqlParameter[0] = new SqlParameter("#p_BO_ID", "KKHG");
objSqlParameter[0].Value = stringInfo[0];
objSqlParameter[1] = new SqlParameter("#p_HRN_ID", "A00025");
objSqlParameter[1].Value = objDataRow["HRN_ID"].ToString();
objSqlParameter[2] = new SqlParameter("#p_Short_Name", "Gowtham");
objSqlParameter[2].Value = objDataRow["Short_Name"].ToString();
objSqlParameter[3] = new SqlParameter("#p_Gender", "Male");
objSqlParameter[3].Value = objDataRow["Gender"].ToString();
objSqlParameter[4] = new SqlParameter("#p_DOB", "05/06/1992");
objSqlParameter[4].Value = objDataRow["DOB"].ToString();
objSqlParameter[5] = new SqlParameter("#p_Street_Name1", "Moovendar Nagar");
objSqlParameter[5].Value = objDataRow["Street_Name1"].ToString();
objSqlParameter[6] = new SqlParameter("#p_Ph_No1", "9585188641");
objSqlParameter[6].Value = objDataRow["Ph_No1"].ToString();
objSqlParameter[7] = new SqlParameter("#p_VIP_Status", "Yes");
objSqlParameter[7].Value = objDataRow["VIP_Status"].ToString();
objSqlParameter[8] = new SqlParameter("#p_Death_Status", "Will");
objSqlParameter[8].Value = objDataRow["Death_Status"].ToString();
objSqlParameter[9] = new SqlParameter("#p_Trans_Status", "test");
objSqlParameter[9].Value = objDataRow["Trans_Status"].ToString();
objSqlParameter[10] = new SqlParameter("#p_Citizenship", "Indian");
objSqlParameter[10].Value = objDataRow["Citizenship"].ToString();
objSqlParameter[11] = new SqlParameter("#p_Adm_Date", "05/06/1992");
objSqlParameter[11].Value = objDataRow["Adm_Date"].ToString();
objSqlParameter[12] = new SqlParameter("#p_Ref_Case", "NA");
objSqlParameter[12].Value = objDataRow["Ref_Case"].ToString();
objSqlParameter[13] = new SqlParameter("#p_Blk", "");
objSqlParameter[13].Value = objDataRow["Blk"].ToString();
objSqlParameter[14] = new SqlParameter("#p_Level_No", "25");
objSqlParameter[14].Value = objDataRow["Level_No"].ToString();
objSqlParameter[15] = new SqlParameter("#p_Unit_No", "25");
objSqlParameter[15].Value = objDataRow["Unit_No"].ToString();
objSqlParameter[16] = new SqlParameter("#p_Street_Name2", "Geetha Nagar");
objSqlParameter[16].Value = objDataRow["Street_Name2"].ToString();
objSqlParameter[17] = new SqlParameter("#p_City", "Thanjavoor City");
objSqlParameter[17].Value = objDataRow["City"].ToString();
objSqlParameter[18] = new SqlParameter("#p_Geographical_Area", "Thanjavoor");
objSqlParameter[18].Value = objDataRow["Geographical_Area"].ToString();
objSqlParameter[19] = new SqlParameter("#p_District", "Thanjavoor");
objSqlParameter[19].Value = objDataRow["District"].ToString();
objSqlParameter[20] = new SqlParameter("#p_Country", "India");
objSqlParameter[20].Value = objDataRow["Country"].ToString();
objSqlParameter[21] = new SqlParameter("#p_Post_Code", "636111");
objSqlParameter[21].Value = objDataRow["Post_Code"].ToString();
objSqlParameter[22] = new SqlParameter("#p_Ph_No2", "8792188641");
objSqlParameter[22].Value = objDataRow["Ph_No2"].ToString();
objSqlParameter[23] = new SqlParameter("#p_Ph_No3", "9626222891");
objSqlParameter[23].Value = objDataRow["Ph_No3"].ToString();
objSqlParameter[24] = new SqlParameter("#p_Ph_No4", "");
objSqlParameter[24].Value = objDataRow["Ph_No4"].ToString();
objSqlParameter[25] = new SqlParameter("#p_Ph_No5", "");
objSqlParameter[25].Value = objDataRow["Ph_No5"].ToString();
objSqlParameter[26] = new SqlParameter("#p_Ph_No6", "");
objSqlParameter[26].Value = objDataRow["Ph_No6"].ToString();
objSqlParameter[27] = new SqlParameter("#p_Email", "gtusk55#gmail.com");
objSqlParameter[27].Value = objDataRow["Email"].ToString();
objSqlParameter[28] = new SqlParameter("#p_Death_Date", "05/06/2085");
objSqlParameter[28].Value = objDataRow["Death_Date"].ToString();
objSqlParameter[29] = new SqlParameter("#p_Discharge_Disposition", "");
objSqlParameter[29].Value = objDataRow["Discharge_Disposition"].ToString();
objSqlParameter[30] = new SqlParameter("#p_Long_Name", "");
objSqlParameter[30].Value = objDataRow["Long_Name"].ToString();
objSqlParameter[31] = new SqlParameter("#p_Remarks", "");
objSqlParameter[31].Value = objDataRow["Remarks"].ToString();
objSqlParameter[47] = new SqlParameter("#p_delmark", "");
objSqlParameter[47].Value = objDataRow["delmark"].ToString();
objSqlParameter[48] = new SqlParameter("#p_gw_status", "");
objSqlParameter[48].Value = objDataRow["gw_status"].ToString();
objSqlParameter[49] = new SqlParameter("#p_data_source", "");
objSqlParameter[49].Value = objDataRow["data_source"].ToString();
objSqlParameter[50] = new SqlParameter("#p_Created_At", "");
objSqlParameter[50].Value = objDataRow["data_source"].ToString();
objSqlParameter[51] = new SqlParameter("#p_Created_By", "");
objSqlParameter[51].Value = objDataRow["data_source"].ToString();
objSqlParameter[52] = new SqlParameter("#p_Created_On", "");
objSqlParameter[52].Value = objDataRow["data_source"].ToString();
objSqlParameter[53] = new SqlParameter("#p_Modified_At", "");
objSqlParameter[53].Value = objDataRow["data_source"].ToString();
objSqlParameter[54] = new SqlParameter("#p_Modified_By", "");
objSqlParameter[54].Value = objDataRow["data_source"].ToString();
objSqlParameter[55] = new SqlParameter("#p_Modified_On", "");
objSqlParameter[55].Value = objDataRow["data_source"].ToString();
objSqlParameter[56] = new SqlParameter("#p_Temp_ID", "");
objSqlParameter[56].Value = objDataRow["Temp_ID"].ToString();
objSqlParameter[57] = new SqlParameter("#p_Temp_Flg", "");
objSqlParameter[57].Value = objDataRow["Temp_Flg"].ToString();
objSqlParameter[58] = new SqlParameter("#p_Latest_HRN", "");
objSqlParameter[58].Value = objDataRow["Latest_HRN"].ToString();
objSqlParameter[59] = new SqlParameter("#p_Latest_HRN", "");
objSqlParameter[59].Direction = ParameterDirection.Output;
// objSqlParameter[60] = new SqlParameter("#p_error_code", "");
//objSqlParameter[60].Direction = ParameterDirection.Output;
objSqlParameter[60] = new SqlParameter("#p_error_msg", "");
objSqlParameter[60].Direction = ParameterDirection.Output;
objSqlParameter[61] = new SqlParameter("#p_result", "");
objSqlParameter[61].Direction = ParameterDirection.Output;
objSqlParameter[62] = new SqlParameter("#p_data_source", "");
objSqlParameter[62].Direction = ParameterDirection.Input;
objSqlParameter[63] = new SqlParameter("#p_dml_indicator", "");
objSqlParameter[63].Value = objDataRow["dml_indicator"].ToString();
objSqlCommand.Parameters.AddRange(objSqlParameter);
objSqlDataAdapter = new SqlDataAdapter(objSqlCommand);
objSqlDataAdapter.Fill(objDataSet);
for (intRowIndex = 0; intRowIndex < objDataSet.Tables[0].Rows.Count - 1; intRowIndex++)
{
MessageBox.Show(objDataSet.Tables[0].Rows[intRowIndex][0].ToString());
}
}
Here this is my code, and tried lot, if i solve one error, another one will occur, here what is the problem to my code? i am going correct or not?
friends am new to this code field, so help me
thanks in advance friends
Ok, here's an example of how to create a connection, call a stored procedure and fill a dataset, which ultimately is what I think you're trying to achieve. You'll obviously need to tweak it for your data but hopefully this will get you on your way:
stringConnectionString = ("Data Source=192.161.0.0;Initial Catalog=kkh_final;Persist Security Info=True;User ID=xx;Password=yyyyyyy");
DataSet objDataSet = new DataSet();
using (SqlConnection connection = new SqlConnection(stringConnectionString))
{
SqlCommand cmd = new SqlCommand("dbo.MyProcedure", connection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter pdParam = new SqlParameter("#YourParam", SqlDbType.VarChar);
pdParam.Value = pd;
cmd.Parameters.Add(pdParam);
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
da.Fill(objDataSet);
}
catch (Exception ex)
{
string error = ex.Message;
}
}
Related
i'm trying to get multiple values from database in return, i write a code and it return me only one value but i want all values in return. i didn't know how to solve it because i'm beginner in c#. here is code
string CommandText = "SELECT * FROM SV WHERE [SVCode]=#id";
using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
{
cmd.Parameters.AddWithValue("#id", sve.SVID);
conn.Open();
DataTable dt = new DataTable();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
sve.SVName = dr["SVName"].ToString();
sve.SVCity = dr["SVCity"].ToString();
sve.SVState = dr["SVState"].ToString();
sve.SVEmail = dr["SVEmail"].ToString();
sve.SVWebsite = dr["SVWebsite"].ToString();
sve.SVAddress = dr["SVAddress"].ToString();
sve.SVNote = dr["SVNote"].ToString();
}
return sve.SVName; //how to return SVCity,SVState,SVEmail,SVWebsite,SVAddress,SVNote
}
private string SVCB(){
SVE sve = new SVE();
sve.SVID = MSVD_vendorcode.SelectedItem.ToString();
sve.SVName = MSVD_vendorname.Text;
sve.SVCity = MSVD_vendorcity.Text;
sve.SVState = MSVD_vendorstate.Text;
sve.SVEmail = MSVD_vendoremail.Text;
sve.SVWebsite = MSVD_vendorwebsite.Text;
sve.SVAddress = MSVD_vendoraddress.Text;
sve.SVNote = MSVD_vendornote.Text;
SVSCB sv = new SVSCB();
return sv.SVCBSI(sve);}
private void SVcode_SelectedIndexChanged(object sender, EventArgs e)
{
MSVname.Text = SVCB();
MSVcity.Text = SVCB();
MSVstate.Text = SVCB();
MSVemail.Text = SVCB();
MSVwebsite.Text = SVCB();
MSVaddress.Text = SVCB();
MSVnote.Text = SVCB();
}
this code working but show only one value of SVName in every textbox.
Update!!
public static string SV_CBSI(SVE sve)
{
using (SQLiteConnection conn = new SQLiteConnection(DatabaseConnection.ConnectionString()))
{
string CommandText = "SELECT * FROM SV WHERE [SVCode]=#id";
using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
{
cmd.Parameters.AddWithValue("#id", sve.SVID);
conn.Open();
DataTable dt = new DataTable();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
sve.SVName = dr["SVName"].ToString();
sve.SVCity = dr["SVCity"].ToString();
sve.SVState = dr["SVState"].ToString();
sve.SVEmail = dr["SVEmail"].ToString();
sve.SVWebsite = dr["SVWebsite"].ToString();
sve.SVAddress = dr["SVAddress"].ToString();
sve.SVNote = dr["SVNote"].ToString();
}
return sve.SVName; //how to return SVCity,SVState,SVEmail,SVWebsite,SVAddress,SVNote
}
}
}
public static string SVCBSI(SVE sve)
{
return SVWCB.SV_CBSI(sve);
}
here is complete code
You need to have a method that return the whole SVE retrieved from database given a particular SVCode.
Notice that I suppose that SVCode is an integer. If not change the input type for this method accordingly
public SVE SV_CBSI(int id)
{
SVE localSve = new SVE();
string CommandText = "SELECT * FROM SV WHERE [SVCode]=#id";
using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
{
cmd.Parameters.AddWithValue("#id", id);
conn.Open();
DataTable dt = new DataTable();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
da.Fill(dt);
if(dt.Rows.Count > 0)
{
localSve.SVName = dr["SVName"].ToString();
localSve.SVCity = dr["SVCity"].ToString();
localSve.SVState = dr["SVState"].ToString();
localSve.SVEmail = dr["SVEmail"].ToString();
localSve.SVWebsite = dr["SVWebsite"].ToString();
localSve.SVAddress = dr["SVAddress"].ToString();
localSve.SVNote = dr["SVNote"].ToString();
}
return localSve;
}
}
Now you can call this method and get back the reference to an SVE instance filled with your data.
private void SVcode_SelectedIndexChanged(object sender, EventArgs e)
{
SVE currentSVE = SV_CBSI(Convert.ToInt32(SVCode.SelectedItem))
MSVname.Text = currentSVE.SVName;
MSVcity.Text = currentSVE.SVCity;
MSVstate.Text = currentSVE.State;
MSVemail.Text = currentSVE.SVEmail;
MSVwebsite.Text = currentSVE.SVWebsite;
MSVaddress.Text = currentSVE.SVAddress;
MSVnote.Text = currentSVE.SVNote;
}
May be you need to return an array (or a List) of strings?
I'm trying to send a JSON format through ActiveMQ (C# Console Application), but right now I'm stock in the following:
If I send the direct JSON serialization (as string) the system adds backward slashes "\" to the text, which makes my listener unable to process the information.
I can remove the backward slashes by doing a JToken Parse, but I cannot send the var on ActiveMQ (ITextMessage requests a String, I'm sending a JToken) and of course if I do cast of the JToken to string I found again the backward slashes "\"
Any clue on how I can achieve my goal???
String IP = "172.29.75.43:61616";
String QueuesNameESF = "queue://MES2WMSTVOFFLINE";
try
{
Uri _uri = new Uri(String.Concat("activemq:tcp://" + IP));
IConnectionFactory factory = new ConnectionFactory(_uri);
using (IConnection conn = factory.CreateConnection())
{
using (ISession session = conn.CreateSession())
{
IDestination destination = SessionUtil.GetDestination(session, QueuesNameESF);
using (IMessageProducer producer = session.CreateProducer(destination))
{
SqlConnection dbConnection = new SqlConnection("Integrated Security=false;Data Source=smxsql08svr01;initial catalog=BSS;user id=sa;password=newshamu;Max Pool Size=75000");
SqlConnection internalConnection = new SqlConnection("Integrated Security=false;Data Source=smxsql08svr01;initial catalog=BSS;user id=sa;password=newshamu;Max Pool Size=75000");
String SearchPallet;
SqlCommand cmd = new SqlCommand();
dbConnection.Open();
SearchPallet = "";
SearchPallet = "Select Replace(Replace(Replace((Convert(varchar,GETDATE(),120) + Palletnum),'-',''),' ',''),':','') as 'ID', ";
SearchPallet += "'Y' as 'beFull','ZJ' as 'ext1','' as 'ext2','Y'as 'isCM',Part_Number as 'itemCode',linea as 'line',";
SearchPallet += "Case When len(Part_Number) = 12 then RIGHT(Part_Number,7) else RIGHT(Part_Number,8) end as 'lot',";
SearchPallet += "Palletnum as 'mainPallet',GETDATE() as 'sendTime','BSS' as'sender','8170' as'werks' From Finishgoods ";
//Remove if not test
SearchPallet += "Where PalletNum = #Palletnum GROUP by PalletNum,Part_Number,linea";
cmd.CommandText = SearchPallet;
cmd.Connection = dbConnection;
cmd.Parameters.Add("#Palletnum", SqlDbType.VarChar).Value = "PT01929734";//Pallet;//"PT01929734";
SqlDataReader rdr = cmd.ExecuteReader();
FirstLevel product = new FirstLevel();
if (rdr.Read())
{
//FirstLevel product = new FirstLevel();
product.guid = rdr.GetValue(0).ToString();
product.beFull = rdr.GetValue(1).ToString();
SecondLevel msg = new SecondLevel();
msg.carton = rdr.GetValue(8).ToString();
List<ThirdLevel> serials = new List<ThirdLevel>();
ThirdLevel sn = new ThirdLevel();
String SearchSN = "Select barcodenum as 'serNo',''as 'ext1',''as 'ext2' From Finishgoods where palletnum = #palletnum";
SqlCommand cmd_sn = new SqlCommand();
internalConnection.Open();
cmd_sn.Connection = internalConnection;
cmd_sn.CommandText = SearchSN;
cmd_sn.Parameters.Add("#Palletnum", SqlDbType.VarChar).Value = "PT01929734";
SqlDataReader rdr_sn = cmd_sn.ExecuteReader();
while (rdr_sn.Read())
{
sn.serNo = rdr_sn.GetValue(0).ToString();
sn.ext1 = rdr_sn.GetValue(1).ToString();
sn.ext2 = rdr_sn.GetValue(2).ToString();
serials.Add(sn);
}
rdr_sn.Close();
cmd_sn.Parameters.Clear();
cmd_sn.Dispose();
internalConnection.Close();
msg.serNoMsg = serials;
product.crtonMsg = msg;
product.ext1 = rdr.GetValue(2).ToString();
product.ext2 = rdr.GetValue(3).ToString();
product.isCM = rdr.GetValue(4).ToString();
product.itemCode = rdr.GetValue(5).ToString();
product.line = rdr.GetValue(6).ToString();
product.lot = rdr.GetValue(7).ToString();
product.mainPallet = rdr.GetValue(8).ToString();
product.sendTime = rdr.GetValue(9).ToString();
product.sender = rdr.GetValue(10).ToString();
product.werks = rdr.GetValue(11).ToString();
}
rdr.Close();
string json_ = JsonConvert.SerializeObject(product);
var json = JToken.Parse(json_);
conn.Start();
ITextMessage request = session.CreateTextMessage(json); //Here I need to send the JSON
producer.Send(request);
}
Console.WriteLine("send succeed");
Console.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine("Receive Failed");
Console.ReadLine();
}
I am trying to keep appending a list of values to lookaheadRunInfo.gerrits until I get a new lookaheadRunInfo.ECJobLink in the while loop,I tried to create a variable “ECJoblink_previous” to capture the previous ECJoblink and create a new list only when they are different and keep appending until ECJoblink_previous changes,I tried as below but its not working,what am I missing?
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = #"some query";
var ECJoblink_previous ="";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//Console.WriteLine(rdr[0] + " -- " + rdr[1]);
//Console.ReadLine();
lookaheadRunInfo.ECJobLink = rdr.GetString(0);
if (ECJoblink_previous == lookaheadRunInfo.ECJobLink)
{
//Keep appending the list of gerrits until we get a new lookaheadRunInfo.ECJobLink
var gerritList = new List<String>();
lookaheadRunInfo.gerrits = gerritList.Add(rdr.GetString(2));
}
ECJoblink_previous = lookaheadRunInfo.ECJobLink;
lookaheadRunInfo.UserSubmitted = rdr.GetString(2);
lookaheadRunInfo.SubmittedTime = rdr.GetString(3).ToString();
lookaheadRunInfo.RunStatus = "null";
lookaheadRunInfo.ElapsedTime = (DateTime.UtcNow - rdr.GetDateTime(3)).ToString();
lookaheadRunsInfo.Add(lookaheadRunInfo);
}
rdr.Close();
}
var lookaheadRunsInfo = new List<LookaheadRunInfo>();
LookAheadRunInfo lookaheadRunInfo;
var i = 0;
var ecJoblink_previous = string.Empty;
while (rdr.Read())
{
if (rdr.GetString(0) != ecJoblink_previous)
{
ecJoblink_previous = rdr.GetString(0);
if (i > 0)
{
lookaheadRunsInfo.Add(lookaheadRunInfo);
}
// Create a new lookaheadRunInfo
lookaheadRunInfo = new lookaheadRunInfo
{
ECJobLink = rdr.GetString(0),
UserSubmitted = rdr.GetString(2),
SubmittedTime = rdr.GetString(3).ToString(),
RunStatus = "null",
ElapsedTime = (DateTime.UtcNow - rdr.GetDateTime(3)).ToString(),
gerrits = new List<string>
{
rdr.GetString(2)
}
};
}
else
{
//Keep appending the list of gerrits until we get a new lookaheadRunInfo.ECJobLink
lookaheadRunInfo.gerrits.Add(rdr.GetString(2));
}
}
I have tried this below code, but it throws a unique key constraint error. I would like it as a pop-up message; see my below code.
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
var fromdate = DateTime.Parse(txtfromdate.Text);
var todate = DateTime.Parse(txttodate.Text);
var datedif1 = (todate - fromdate).Days;
var sqlInsert = new SqlCommand("INSERT INTO datelist ([datedif],[batch],[daywk],[semester],[weekbatch],[subject],[facultyname],[facultyid],[WeekMonth]) VALUES (#datedif,#batch,#daywk,#semester,#weekbatch,#subject,#facultyname,#facultyid,#weekMonth)", con2);
var sqlParamater = sqlInsert.Parameters.Add("#datedif", SqlDbType.Date);
var sqlParameter1 = sqlInsert.Parameters.Add("#batch", SqlDbType.NVarChar);
var sqlParameter2 = sqlInsert.Parameters.Add("#daywk", SqlDbType.NVarChar);
var sqlParameter3 = sqlInsert.Parameters.Add("#semester", SqlDbType.NVarChar);
var sqlParameter4 = sqlInsert.Parameters.Add("#weekbatch", SqlDbType.NVarChar);
var sqlParameter5 = sqlInsert.Parameters.Add("#subject", SqlDbType.NVarChar);
var sqlParameter6 = sqlInsert.Parameters.Add("#facultyname", SqlDbType.NVarChar);
var sqlParameter7 = sqlInsert.Parameters.Add("#facultyid", SqlDbType.NVarChar);
var sqlParameter8 = sqlInsert.Parameters.Add("#WeekMonth", SqlDbType.NVarChar);
con2.Open();
try
{
for (var i = 0; i <= datedif1; i++)
{
var consecutiveDate = fromdate.AddDays(i);
sqlParamater.Value = consecutiveDate;
sqlParameter1.Value = batch1;
sqlParameter2.Value = dayweek;
sqlParameter3.Value = semester;
sqlParameter4.Value = weekbatch;
sqlParameter5.Value = subject;
sqlParameter6.Value = faculty;
sqlParameter7.Value = facultyid;
sqlParameter8.Value = weekmonth;
int s = sqlInsert.ExecuteNonQuery();
}
con2.Close();
}
catch(ConstraintException ex)
{
throw new ApplicationException("data are duplicated");
}
}
Here's a screen shot of the error which am getting,
You are catching a ConstraintException, but the insert is throwing a SqlException which is not a subclass of ConstraintException. Change the catch and you will get it handled.
another sql problem of mine ..
this time the reader doesn't work properly ( I think so )
I use this code and I get only 1 record added and my db has 100`s of records ..
public void addPosts()
{
string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\msgdb.sdf";
string sql;
PictureBox avaBox = new PictureBox();
PictureBox pictureBox1 = new PictureBox();
Button atBtn = new Button();
RichTextBox msgBox = new RichTextBox();
Panel panelz = new Panel();
DateTime dt = DateTime.Now;
SqlCeConnection connection = new SqlCeConnection("datasource=" + dbfile);
// Read all rows from the table test_table into a dataset (note, the adapter automatically opens the connection)
SqlCeDataAdapter adapter = new SqlCeDataAdapter("select * from posts", connection);
DataSet data = new DataSet();
adapter.Fill(data);
SqlCeCommand cmd = new SqlCeCommand();
cmd.Connection = connection;
sql = "Select user_from,msg,avatar FROM posts";
cmd.CommandText = sql;
connection.Open();
SqlCeDataReader reader = cmd.ExecuteReader();
int i = 0;
while (reader.Read())
{
i++;
string ava = reader.GetString(2);
string usrFrom = reader.GetString(0);
string messige = reader.GetString(1);
//
// groupBox1
//
panelz.Controls.Add(pictureBox1);
panelz.Controls.Add(atBtn);
panelz.Controls.Add(avaBox);
panelz.Controls.Add(msgBox);
panelz.Location = new System.Drawing.Point(0, 335);
panelz.Name = "panel"+ i;
panelz.Size = new System.Drawing.Size(335, 90);
panelz.TabIndex = 0;
panelz.TabStop = false;
//
// pictureBox1
//
pictureBox1.Dock = System.Windows.Forms.DockStyle.Right;
pictureBox1.Image = global::m23.Properties.Resources.post_area;
pictureBox1.Location = new System.Drawing.Point(58, 0);
pictureBox1.Name = "pictureBox1"+i;
pictureBox1.Size = new System.Drawing.Size(281, 99);
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
pictureBox1.TabIndex = 1;
pictureBox1.TabStop = false;
//
// atBtn
//
atBtn.AutoSize = true;
atBtn.BackgroundImage = global::m23.Properties.Resources.post_user;
atBtn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
atBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
atBtn.Location = new System.Drawing.Point(0, 62);
atBtn.Name = "atBtn"+i;
atBtn.Size = new System.Drawing.Size(28, 25);
atBtn.TabIndex = 2;
atBtn.UseVisualStyleBackColor = true;
//
avaBox.Location = new System.Drawing.Point(0, 0);
avaBox.Name = "avaBox"+i;
avaBox.Size = new System.Drawing.Size(53, 53);
avaBox.TabIndex = 4;
avaBox.TabStop = false;
avaBox.ImageLocation = "http://img.edno23.com/avatars/thumbs/" + ava;
//
msgBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
msgBox.Location = new System.Drawing.Point(76, 10);
msgBox.Name = "msgBox"+i;
msgBox.ReadOnly = true;
msgBox.BackColor = Color.White;
msgBox.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None;
msgBox.Size = new System.Drawing.Size(251, 68);
msgBox.TabIndex = 3;
msgBox.Text = messige;
msgBox.BringToFront();
//
CommonFlowPanel.Controls.Add(panelz);
}
connection.Close();
}
Thanks for the help in advance!
Put the declarations for the Panel and all the controls that go onto each panel inside your while loop. You're basically re-adding your once instance of Panel ("panelz") over and over again. What you want to do is create a new panel for each row, inside your while loop, along with new instances of each control that sits on the panel.