Invalid Attempt To Read Data when no Data is Present [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Values Not Being Stored to Database
cmd = new SqlCommand("ImportExcel", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#UserCategory", SqlDbType.NVarChar, 100).Value = imp.UserCategory;
cmd.Parameters.Add("#CompanyName", SqlDbType.NVarChar, 200).Value = imp.CompanyName;
cmd.Parameters.Add("#PrimaryContact", SqlDbType.NVarChar, 200).Value = imp.PrimaryContact;
cmd.Parameters.Add("#Title", SqlDbType.NVarChar, 50).Value = imp.Title;
cmd.Parameters.Add("#FirstName", SqlDbType.NVarChar, 100).Value = imp.FirstName;
cmd.Parameters.Add("#LastName", SqlDbType.NVarChar, 100).Value = imp.LastName;
cmd.Parameters.Add("#LeadIndustryType", SqlDbType.NVarChar, 100).Value = imp.IndustruType;
cmd.Parameters.Add("#SupplierService", SqlDbType.NVarChar).Value = imp.ServiceCategory;
cmd.Parameters.Add("#Designation", SqlDbType.NVarChar, 100).Value = imp.Designation;
// cmd.Parameters.Add("#AnnualRevenue", SqlDbType.NVarChar, 50).Value = ContactsObj.AnnualRevenue;
cmd.Parameters.Add("#PhoneCountryCode", SqlDbType.NVarChar, 50).Value = imp.PhoneCountryCode;
cmd.Parameters.Add("#PhoneNumber", SqlDbType.NVarChar, 50).Value = imp.PhoneNumber;
cmd.Parameters.Add("#MobileCountryCode", SqlDbType.NVarChar, 50).Value = imp.MobileCountryCode;
cmd.Parameters.Add("#MobileNumber", SqlDbType.NVarChar, 50).Value = imp.MobileNumber;
cmd.Parameters.Add("#Email", SqlDbType.NVarChar, 200).Value = imp.Email;
if (!String.IsNullOrEmpty(imp.EmailOption))
{
cmd.Parameters.Add("#EmailOption", SqlDbType.Bit).Value = imp.EmailOption;
}
else
{
cmd.Parameters.Add("#EmailOption", SqlDbType.Bit).Value = DBNull.Value;
}
if (!String.IsNullOrEmpty(imp.OfferAlertReceive))
{
cmd.Parameters.Add("#OfferAlertReceive", SqlDbType.Bit).Value = imp.OfferAlertReceive;
}
else
{
cmd.Parameters.Add("#OfferAlertReceive", SqlDbType.Bit).Value = DBNull.Value;
}
//cmd.Parameters.Add("#SecondaryEmail", SqlDbType.NVarChar, 200).Value = imp.SecondaryEmail;
cmd.Parameters.Add("#SkypeID", SqlDbType.NVarChar, 200).Value =imp.SkypeID;
cmd.Parameters.Add("#Fax", SqlDbType.NVarChar, 50).Value = imp.Fax;
cmd.Parameters.Add("#Website", SqlDbType.NVarChar, 200).Value = imp.Website;
cmd.Parameters.Add("#PresentAddressLine1", SqlDbType.NVarChar, 200).Value =imp.PresentAddressLine1;
cmd.Parameters.Add("#PresentAddressLine2", SqlDbType.NVarChar, 200).Value =imp.PresentAddressLine2;
cmd.Parameters.Add("#PresentCity", SqlDbType.NVarChar, 200).Value =imp.PresentCity;
cmd.Parameters.Add("#PresentState", SqlDbType.NVarChar, 100).Value = imp.PresentState;
cmd.Parameters.Add("#PresentCountry", SqlDbType.NVarChar, 100).Value = imp.PresentCountry;
cmd.Parameters.Add("#PresentZipCode", SqlDbType.NVarChar, 50).Value = imp.PresentZipCode;
cmd.Parameters.Add("#PermanentAddressLine1", SqlDbType.NVarChar, 200).Value = imp.PermanentAddressLine1;
cmd.Parameters.Add("#PermanentAddressLine2", SqlDbType.NVarChar, 200).Value = imp.PermanentAddressLine2;
cmd.Parameters.Add("#PermanentCity", SqlDbType.NVarChar, 200).Value = imp.PermanentCity;
cmd.Parameters.Add("#PermanentState", SqlDbType.NVarChar, 100).Value = imp.PermanentState;
if (!String.IsNullOrEmpty(imp.PermanentCountry))
{
cmd.Parameters.Add("#PermanentCountry", SqlDbType.NVarChar, 100).Value = imp.PermanentCountry;
}
else
{
cmd.Parameters.Add("#PermanentCountry", SqlDbType.NVarChar, 100).Value = DBNull.Value;
}
cmd.Parameters.Add("#PermanentZipCode", SqlDbType.NVarChar, 50).Value = imp.PermanentZipCode;
if (!String.IsNullOrEmpty(imp.GroupName))
{
cmd.Parameters.Add("#OfferAlertType", SqlDbType.NVarChar, 100).Value = imp.OfferAlertType;
}
else
{
cmd.Parameters.Add("#OfferAlertType", SqlDbType.NVarChar, 100).Value = DBNull.Value;
}
if (!String.IsNullOrEmpty(imp.GroupName))
{
cmd.Parameters.Add("#GroupName", SqlDbType.NVarChar, 100).Value = imp.GroupName;
}
else
{
cmd.Parameters.Add("#GroupName", SqlDbType.NVarChar, 100).Value = DBNull.Value;
}
cmd.Parameters.Add("#LeadSource",SqlDbType.NVarChar,100).Value=imp.LeadSource;
cmd.Parameters.Add("#Description", SqlDbType.NVarChar, 300).Value = imp.Description;
cmd.Parameters.Add("#CreatedBy", SqlDbType.NVarChar, 100).Value = imp.CreatedBy;
cmd.Parameters.Add("#ip", SqlDbType.NVarChar, 50).Value = imp.ip;
con.Open();
SqlDataReader dr1 = cmd.ExecuteReader();
//dr1 = cmd.ExecuteReader();
string st = string.Empty;
while (dr1.Read())
{
st = dr1[0].ToString();
}
st = dr1[0].ToString();
return st;

Remove this line after your while loop:-
st = dr1[0].ToString();
You need to call the Read() method to actually read the date from DataReader object.
Also, it would be better if you will wrap your code within using block:-
using (SqlDataReader dr1 = cmd.ExecuteReader())
{
while (dr1.Read())
{
st = dr1[0].ToString(); //With Index
st = dr1["ColumnName"].ToString(); //With Column Name
}
}
Update:
Based on your comment, this is an Insert SP and you were trying to read the return value so you can't use SqlDataReader, if you want to return whether SP successfully inserted data, you can return status code like 1 for successfull insertion & 0 for failure or something. You can then read this return code like this:-
SqlParameter returnParameter = cmd.Parameters.Add("StatusCd", SqlDbType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
int statusCode = (int)returnParameter.Value;
But if you want to return a text like you have mentioned Successfully Inserted, you need to add that as Output parameter because SQL can only return interger values, you can then retrieve this output value as:-
SqlParameter output = new SqlParameter("#code", SqlDbType.Int);
output.Direction = ParameterDirection.Output;
cmd.Parameters.Add(output);
cmd.ExecuteNonQuery();
st = output.Value.ToString();
Here, #code will be the ouput parameter in your SP, which you'll set as: SET #code = 'Insert successfull'.

Related

How to get the checked items string in a CheckListBox

foreach (var item in clbSymptoms.CheckedItems)
{
using (var cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add("#fName", SqlDbType.VarChar, 50).Value = txtFName.Text.Substring(0, 1).ToUpper() + txtFName.Text.Substring(1);
cmd.Parameters.Add("#MName", SqlDbType.VarChar, 50).Value = txtMName.Text.Substring(0, 1).ToUpper() + txtMName.Text.Substring(1);
cmd.Parameters.Add("#lName", SqlDbType.VarChar, 50).Value = txtLName.Text.ToUpper();
cmd.Parameters.Add("#patientAge", SqlDbType.Int).Value = txtAge.Text;
cmd.Parameters.Add("#purok", SqlDbType.VarChar, 50).Value = cbPurok.Text;
cmd.Parameters.Add("#caseClass", SqlDbType.VarChar, 50).Value = cbClass.Text;
cmd.Parameters.Add("#q1", SqlDbType.VarChar, 50).Value = cbQuestion1.Checked;
cmd.Parameters.Add("#q2", SqlDbType.VarChar, 50).Value = cbQuestion2.Checked;
cmd.Parameters.Add("#symptoms", SqlDbType.VarChar, 50).Value = item.ToString();
cmd.Parameters.Add("#date", SqlDbType.DateTime).Value = DateTime.Now;
cmd.ExecuteNonQuery();
}
}
I want the symptoms to have their own cell but the name and other things to be a single cell only.
Loop through your checked items like this:
foreach(var item in clbSymptoms.CheckedItems)
{
using (var cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add("#fName", SqlDbType.VarChar, 50).Value = txtFName.Text.Substring(0, 1).ToUpper() + txtFName.Text.Substring(1);
cmd.Parameters.Add("#MName", SqlDbType.VarChar, 50).Value = txtMName.Text.Substring(0, 1).ToUpper() + txtMName.Text.Substring(1);
cmd.Parameters.Add("#lName", SqlDbType.VarChar, 50).Value = txtLName.Text.ToUpper();
cmd.Parameters.Add("#patientAge", SqlDbType.Int).Value = txtAge.Text;
cmd.Parameters.Add("#purok", SqlDbType.VarChar, 50).Value = cbPurok.Text;
cmd.Parameters.Add("#caseClass", SqlDbType.VarChar, 50).Value = cbClass.Text;
cmd.Parameters.Add("#questionnaire", SqlDbType.VarChar, 50).Value = "(1)" + cbQuestion1.Checked + " " + "(2)" + cbQuestion2.Checked;
cmd.Parameters.Add("#symptoms", SqlDbType.VarChar, 50).Value = item.ToString();
cmd.Parameters.Add("#date", SqlDbType.DateTime).Value = DateTime.Now;
cmd.ExecuteNonQuery();
}

data type mismatch when trying to insert into an Access database from C#

I am trying to insert into an access database. all the fields worked except for the "passwordx" field in which I get data type mismatch in criteria expression.
string commandSql = "INSERT INTO Members ( lastName, firstName, id, dob, phone, areaCodeNo, adress, cityNo, zipCode, email, active,passwordx) VALUES ( #lastName, #firstName, #id, #dob, #phone, #areaCodeNo, #adress, #cityNo, #zipCode, #email, #active,#passwordx); ";
OleDbCommand command = new OleDbCommand(commandSql, connection);
command.Parameters.Add("#lastName", OleDbType.VarChar, 100).Value = mem.lastName;
command.Parameters.Add("#firstName", OleDbType.VarChar, 100).Value = mem.firstName;
command.Parameters.Add("#id", OleDbType.VarChar, 100).Value = mem.id;
command.Parameters.Add("#dob", OleDbType.VarChar, 100).Value = mem.dob;
command.Parameters.Add("#phone", OleDbType.VarChar, 100).Value = mem.phone;
command.Parameters.Add("#areaCodeNo", OleDbType.Integer, 100).Value = mem.areaCodeNo;
command.Parameters.Add("#adress", OleDbType.VarChar, 100).Value = mem.adress;
command.Parameters.Add("#cityNo", OleDbType.Integer, 100).Value = mem.cityNo;
command.Parameters.Add("#zipCode", OleDbType.VarChar, 100).Value = mem.zipCode;
command.Parameters.Add("#email", OleDbType.VarChar, 100).Value = mem.email;
command.Parameters.Add("#passwordx", OleDbType.VarChar, 100).Value = mem.password;
command.Parameters.Add("#active", OleDbType.Boolean, 100).Value = mem.active;
connection.Open();
affectedRows = command.ExecuteNonQuery();
connection.Close();
thanks for the help.
Shuffle the last two parameters to match the sequence of your SQL:
command.Parameters.Add("#active", OleDbType.Boolean, 100).Value = mem.active;
command.Parameters.Add("#passwordx", OleDbType.VarChar, 100).Value = mem.password;
And a Boolean doesn't have a length.

No Mapping Exists from Object Type System.Widows.Forms.DataGridViewTextBoxCell

I'm new to C# coding and this website so bear with me. On the button click I want my stored procedure to execute and populate the datagridview. When I run the application and click the button I keep getting an error saying:
"No mapping exists from object type System.Windows.Forms.DataGridViewTextBoxCell to a known managed provider native type."
How do I fix this? I've tried converting the parameters to their data types which doesn't seem to work either. I've been stuck on this for a while and am struggling to find anything on the internet about my specific error. Thank you.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sqlcn1 = new SqlConnection("My Server Connection String");
sqlcn1.Open();
//Stored Procedure
SqlCommand sqlcmddel = new SqlCommand("My_Stored_Procedure", sqlcn1);
sqlcmddel.CommandType = CommandType.StoredProcedure;
sqlcmddel.ExecuteNonQuery();
sqlcn1 = new SqlConnection("My Server Connection String");
sqlcn1.Open();
foreach (DataGridViewRow row in dataGridView1.Rows) ;
SqlCommand sqlcmdins = new SqlCommand("My_Stored_Procedure", sqlcn1);
//Stored Procedure
sqlcmdins.CommandType = CommandType.StoredProcedure;
sqlcmdins.Parameters.Add("#sugnum", SqlDbType.Int).Value = dataGridView1.Rows[Ournum].Cells[0];
sqlcmdins.Parameters.Add("#sugtype", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[1];
sqlcmdins.Parameters.Add("#buyerid", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[2];
sqlcmdins.Parameters.Add("#duedate", SqlDbType.DateTime).Value = dataGridView1.Rows[Ournum].Cells[3];
sqlcmdins.Parameters.Add("#xrelqty", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[4];
sqlcmdins.Parameters.Add("#purchasingfactor", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[5];
sqlcmdins.Parameters.Add("#relqty", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[6];
sqlcmdins.Parameters.Add("#jobnum", SqlDbType.NVarChar, 20).Value = dataGridView1.Rows[Ournum].Cells[7];
sqlcmdins.Parameters.Add("#assemblyseq", SqlDbType.SmallInt).Value = dataGridView1.Rows[Ournum].Cells[8];
sqlcmdins.Parameters.Add("#jobseq", SqlDbType.SmallInt).Value = dataGridView1.Rows[Ournum].Cells[9];
sqlcmdins.Parameters.Add("#warehousecode", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[10];
sqlcmdins.Parameters.Add("#fob", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[11];
sqlcmdins.Parameters.Add("#shipviacode", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[12];
sqlcmdins.Parameters.Add("#termscode", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[13];
sqlcmdins.Parameters.Add("#vendornum", SqlDbType.Int).Value = dataGridView1.Rows[Ournum].Cells[14];
sqlcmdins.Parameters.Add("#purpoint", SqlDbType.Int).Value = dataGridView1.Rows[Ournum].Cells[15];
sqlcmdins.Parameters.Add("#linedesc", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[16];
sqlcmdins.Parameters.Add("#ium", SqlDbType.NVarChar, 10).Value = dataGridView1.Rows[Ournum].Cells[17];
sqlcmdins.Parameters.Add("#unitprice", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[18];
sqlcmdins.Parameters.Add("#docunitprice", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[19];
sqlcmdins.Parameters.Add("#taxable", SqlDbType.Bit).Value = dataGridView1.Rows[Ournum].Cells[20];
Your issue is that you're trying to pass the Cell into the stored procedure. You need to pass in the cells value.
Also, because you've put a ';' at the end of the foreach call, your loop around the rows isn't doing anything. Assuming you're trying to insert all the rows from the datagridview, you'll need something like this...
using (SqlConnection sqlcn1 = new SqlConnection("My Server Connection String"))
{
sqlcn1.Open();
//Stored Procedure
using (SqlCommand sqlcmddel = new SqlCommand("My_Stored_Procedure", sqlcn1))
{
sqlcmddel.CommandType = CommandType.StoredProcedure;
sqlcmddel.ExecuteNonQuery();
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
using (SqlCommand sqlcmdins = new SqlCommand("My_Stored_Procedure", sqlcn1))
{
sqlcmdins.CommandType = CommandType.StoredProcedure;
//Stored Procedure
sqlcmdins.Parameters.Add("#sugnum", SqlDbType.Int).Value = row.Cells[0].Value;
sqlcmdins.Parameters.Add("#sugtype", SqlDbType.NVarChar, 50).Value = row.Cells[1].Value;
sqlcmdins.Parameters.Add("#buyerid", SqlDbType.NVarChar, 50).Value = row.Cells[2].Value;
sqlcmdins.Parameters.Add("#duedate", SqlDbType.DateTime).Value = row.Cells[3].Value;
sqlcmdins.Parameters.Add("#xrelqty", SqlDbType.Float).Value = row.Cells[4].Value;
sqlcmdins.Parameters.Add("#purchasingfactor", SqlDbType.Float).Value = row.Cells[5].Value;
sqlcmdins.Parameters.Add("#relqty", SqlDbType.Float).Value = row.Cells[6].Value;
sqlcmdins.Parameters.Add("#jobnum", SqlDbType.NVarChar, 20).Value = row.Cells[7].Value;
sqlcmdins.Parameters.Add("#assemblyseq", SqlDbType.SmallInt).Value = row.Cells[8].Value;
sqlcmdins.Parameters.Add("#jobseq", SqlDbType.SmallInt).Value = row.Cells[9].Value;
sqlcmdins.Parameters.Add("#warehousecode", SqlDbType.NVarChar, 50).Value = row.Cells[10].Value;
sqlcmdins.Parameters.Add("#fob", SqlDbType.NVarChar, 50).Value = row.Cells[11].Value;
sqlcmdins.Parameters.Add("#shipviacode", SqlDbType.NVarChar, 50).Value = row.Cells[12].Value;
sqlcmdins.Parameters.Add("#termscode", SqlDbType.NVarChar, 50).Value = row.Cells[13].Value;
sqlcmdins.Parameters.Add("#vendornum", SqlDbType.Int).Value = row.Cells[14].Value;
sqlcmdins.Parameters.Add("#purpoint", SqlDbType.Int).Value = row.Cells[15].Value;
sqlcmdins.Parameters.Add("#linedesc", SqlDbType.NVarChar, 50).Value = row.Cells[16].Value;
sqlcmdins.Parameters.Add("#ium", SqlDbType.NVarChar, 10).Value = row.Cells[17].Value;
sqlcmdins.Parameters.Add("#unitprice", SqlDbType.Float).Value = row.Cells[18].Value;
sqlcmdins.Parameters.Add("#docunitprice", SqlDbType.Float).Value = row.Cells[19].Value;
sqlcmdins.Parameters.Add("#taxable", SqlDbType.Bit).Value = row.Cells[20].Value;
sqlcmdins.ExecuteNonQuery();
}
}
}

Creating a Class for SqlConnection in ASP.NET and then use in your application?

I'm having to select, update, delete, etc. basically everywhere on my web application and every time I'm having to write something like this:
con.Open();
cmd = new SqlCommand("update items set item_cost = #cost, item_retail_value = #retail, item_v_style = #v_style, item_v_color = #v_color, item_description = #description, " +
"item_date_modify = #date, item_time_modify = #time, item_user_modify = #user where item_style = #style and item_color = #color and item_sec_dimenssion = #sec", con);
cmd.Parameters.Add("#style", SqlDbType.VarChar, 15).Value = styl;
cmd.Parameters.Add("#color", SqlDbType.VarChar, 3).Value = colr;
cmd.Parameters.Add("#sec", SqlDbType.VarChar, 8).Value = sdim;
cmd.Parameters.Add("#size", SqlDbType.VarChar, 3).Value = size;
cmd.Parameters.Add("#cost", SqlDbType.VarChar, 8).Value = sprice;
cmd.Parameters.Add("#retail", SqlDbType.VarChar, 8).Value = sretail;
cmd.Parameters.Add("#uom", SqlDbType.VarChar, 3).Value = uom;
cmd.Parameters.Add("#sku", SqlDbType.VarChar, 10).Value = sku;
cmd.Parameters.Add("#barcode", SqlDbType.VarChar, 20).Value = barcode;
cmd.Parameters.Add("#v_style", SqlDbType.VarChar, 100).Value = v_style;
cmd.Parameters.Add("#v_color", SqlDbType.VarChar, 20).Value = v_color;
cmd.Parameters.Add("#description", SqlDbType.VarChar, 40).Value = description;
cmd.Parameters.Add("#date", SqlDbType.VarChar, 20).Value = date;
cmd.Parameters.Add("#time", SqlDbType.VarChar, 20).Value = time;
cmd.Parameters.Add("#user", SqlDbType.VarChar, 20).Value = user;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
Is there a way to create a Class to create SqlConnection with a SqlCommand for "Select", "Update", "Delete", etc. and just provide the table, the fields, parameters and the criteria so I don't have to write all this code every time.
Any help will really appreciate it.
Linq2Sql
Entity Framework
nHibernate
Dapper
BLToolkit
create a class with Singleton and then every time you want to do anything with SqlConnection
you can use the same object.

C# stored procedure with parameters

I am receiving an error in my application and i can not figure out how to resolve it. Here is the code:
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
myConnection.Open();
SqlCommand cmd = new SqlCommand("SELECT ServerIP FROM Servers", myConnection);
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
string serverIP = rdr["ServerIP"].ToString();
ScheduledTasks st = new ScheduledTasks(#"\\" + serverIP);
string[] taskNames = st.GetTaskNames();
foreach (string name in taskNames)
{
Task t = st.OpenTask(name);
var status = t.Status;
var recentRun = t.MostRecentRunTime;
var nextRun = t.NextRunTime;
var appName = t.ApplicationName;
SqlConnection myConnection2 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
SqlCommand myCommand2 = new SqlCommand("sp_AddScheduledTasks", myConnection2);
try
{
myConnection2.Open();
myCommand2.CommandType = CommandType.StoredProcedure;
myCommand2.Parameters.Add("#ID", SqlDbType.Int);
myCommand2.Parameters["#ID"].Direction = ParameterDirection.Output;
myCommand2.Parameters.Add("#ServerIP", SqlDbType.NVarChar, 20).Value = serverIP;
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = t;
myCommand2.Parameters.Add("#MostRecentRun", SqlDbType.DateTime).Value = recentRun;
myCommand2.Parameters.Add("#NextRunTime", SqlDbType.DateTime).Value = nextRun;
myCommand2.Parameters.Add("#AppName", SqlDbType.NVarChar, 50).Value = appName;
myCommand2.Parameters.Add("#Status", SqlDbType.NVarChar, 50).Value = status;
int rows = myCommand2.ExecuteNonQuery();
}
finally
{
myConnection2.Close();
}
The error I am receiving is with the ExecuteNonQuery. It says
InvalidCastException Failed to
convert parameter value from a Task to
a String.
I thought it had something to do with where I put the try (inside the if statement) but I am not sure. Any help would be much appreciated.
Thanks,
Matt
My guess is that in
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = t;
t is not a string?
t is of type Task but when you pass it to your stored procedure you are passing it as nvarchar.
Here is your code:
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = t;
Assuming your name variable is indeed the #TaskName simply use name
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = name;
or if you override ToString()
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = t.ToString();
or if you have Name on your Task object
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = t.Name;
In the line:
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = t;
I think you have to pass t.Name or t.ToString() up on the task's name (I don't know it).
I think the problem is with this line:
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = t;
t cannot be converted to a string. You could try t.Name or t.ToString() (not sure what properties are available on that class off the top of my head.)
T is type of Task and and ado try convert it to string
you should put t.GetType().Name or the real task name here
because we can't pass objects as parameter the only type are known sqltypes
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = t;
t is a Task not a string. This should probably by name.
On the line:
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = t;
't' is a task, not a string. You need to get the task name instead (by the looks of it)
The issue is on this line:
myCommand2.Parameters.Add("#TaskName", SqlDbType.NVarChar, 50).Value = t;
You are passing the task object in instead of the name of the task.

Categories