I’m creating a sql statement in c#, and I am trying to get the value from a text box in a previous form:
SqlParameter PeriodFrom = new SqlParameter("@PeriodFrom", SqlDbType.VarChar);
PeriodFrom.Value =
I am just unsure of what goes after the equals sign. I tried the form name.textbox name.
In the code behind of the form, inside the form class, you can declare a property:
And you can use it with your SqlParameter like this:
Edit:
This wouldn’t work since the Form class object is not static and it wouldn’t persist. To make this work, you would need to make changes to the Program class. You would need to add a static Data member in the Program class like this:
and would need to pass this in the main method as below:
And in the SQLParameter you can pass it like this:
I tried to give you one of the ways to get the value but I do not think this is a good practice. Ideally, for passing values between forms, or layers you need to have some mechanism for storing and retrieving values, like a context, or Session. This is not the solution but a work around. And I take no responsibility if the code gets messed up cause of the above changes. 🙂