My C# code is pasted below:
SqlConnection con = new SqlConnection("Data Source=pawan-pc;Initial Catalog=App91;Integrated Security=True");
try
{
SqlCommand cmd = new SqlCommand("sp_gettinglogsfromdevice", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@tablename","DeviceLogs_"+comboBox_Attendance_SelMonth.Text+"_"+combobox_Attendance_SelectYear.Text);
cmd.Parameters.AddWithValue("@fromduration",datetimepicker_att_From.Value.Date);
cmd.Parameters.AddWithValue("@Toduration",datetimepicker_att_To.Value.Date);
DataRowView drow = (DataRowView)combobox_att_events.SelectedValue;
string l = drow.Row.ItemArray[0].ToString();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = con;
con.Open();
cmd1.CommandText = "select WorkCode from WorkCodes where WorkCodeName='"+l+"'";
SqlDataReader reader = cmd1.ExecuteReader();
dk = new DataTable();
dk.Load(reader);
cmd.Parameters.AddWithValue("@WorkCode", dk.Rows[0][0]);
SqlDataReader reader1 = cmd.ExecuteReader();
DataTable dp =new DataTable();
dp.Load(reader1);
datagridview_att_attendance.DataSource=dp;
The referenced stored procedure is pasted below:
create procedure sp_gettinglogsfromdevice
(
@tablename varchar(75),
@fromduration datetime,
@Toduration datetime,
@WorkCode nvarchar(100)
)
As
Begin
Exec('select UserId,LogDate,WorkCode from '
+@tablename+' where LogDate between '
+@fromduration+' and '+@Toduration+' and WorkCode = '+@WorkCode);
End
Go
Iam actually taking the name of the table dynamically as in my database a table is created automatically with the name DeviceLogs_(month)_(year) for every month of logs that are created.Please help me with this error.I have seen numerous post but i cannot seem to wrap my head along this idea